Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-11-2012, 09:14 PM   PM User | #1
sirsaula
New Coder

 
Join Date: Jun 2012
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
sirsaula is an unknown quantity at this point
PHP variable not incrementing

my variable xt keeps on setting to 20 for all the javascript arrays why is it not incrementing after it stores into the array? Please help thanks!

PHP Code:
<? $xt 0 ?>;
    var j = 0;
while (j < 200) {
 <?
  
while(  $xt 250)
        {            
            if (
$members[$xt]['department'] == $depttts){
            
                break;            
            
            }        
            
            else {
             
$xt++;
            }
            
        }
         
        
?>
        
contacts[j] = {"NAME": "<? 
    
print $xt ?>","TITLE":"number: " + j + " ","CONTACT":"<? print ($members[$xt]['title']).'<br> <b>P: </b>'.($members[$xt]['phone']).'<br> <b>F: </b>'.($members[$xt]['fax']) ?>"}
<? $xt++ ?>;


j++;
}

Last edited by sirsaula; 06-11-2012 at 09:18 PM..
sirsaula is offline   Reply With Quote
Old 06-11-2012, 10:01 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
In PHP your $xt starts with a value of 0. Then you increment $xt until either $xt==250 or the following condition is met
Code:
$members[$xt]['department'] == $depttts
And I guess this is the case for $xt==20. In that case you break the PHP while loop and $xt will always be 20 ... you never change it again. So you create the Javascript code with $xt==20 ...
devnull69 is offline   Reply With Quote
Old 06-11-2012, 10:24 PM   PM User | #3
sirsaula
New Coder

 
Join Date: Jun 2012
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
sirsaula is an unknown quantity at this point
I am not understanding what you mean?


Each of my positions in the contact array has the same persons name in it i need it to move on to the next name
sirsaula is offline   Reply With Quote
Old 06-12-2012, 03:02 AM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,448
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Your loop incrementing $xt doesn't include the contacts[j] line
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 06-12-2012, 09:03 AM   PM User | #5
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Yes, that's exactly what I mean. The line where you use $xt is the line starting with "contacts[j] =". This line is outside the PHP while loop so $xt will always be the same because you only change it INSIDE the PHP while loop (and one more time after the contacts[j] line)

Last edited by devnull69; 06-12-2012 at 09:18 AM..
devnull69 is offline   Reply With Quote
Old 06-12-2012, 09:21 AM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Ah ... I guess I understand your problem. I think you didn't realize that PHP generating Javascript code is a process that has already finished once the Javascript is running. There is no interaction.

So first you create the full Javascript code using PHP and only then(!) the Javascript code will run with the fix values that have been set by PHP. During the Javascript while loop the PHP code "inside" this loop will not be executed ... it has already finished to run.
devnull69 is offline   Reply With Quote
Old 06-12-2012, 04:04 PM   PM User | #7
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
PHP is a server side language and Javascript is a client side language. They are not processed at the same time, or even by the same device (mostly)

When a user makes a request to your host for a site, the server sees the PHP on your page, processes it, and turns it into text (outputs whatever you echo/print/w/e, makes whatever connections, etc), then sends this text to the user. The user's browser then reads the text and figures out what it is, reading the CSS and applying it, reading the HTML as markup, and reading the Javascript, which it then processes.

While there are ways to run JS on the server side (nodeJS), Javascript and PHP have no direct interaction at all... you can't get the two to work "together" per say.
Keleth is offline   Reply With Quote
Old 06-13-2012, 08:05 PM   PM User | #8
sirsaula
New Coder

 
Join Date: Jun 2012
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
sirsaula is an unknown quantity at this point
oh my any idea how I can fix this guys??
sirsaula is offline   Reply With Quote
Old 06-13-2012, 08:36 PM   PM User | #9
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
Fix what? The issue is you can't mix PHP and JS directly like you're trying to do.

I can't even figure out what your code is trying to do, so I have no way of giving you any advice on what to do. All I can say is you can't do what you're trying to do, you need to do something else.
Keleth is offline   Reply With Quote
Old 06-13-2012, 08:38 PM   PM User | #10
sirsaula
New Coder

 
Join Date: Jun 2012
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
sirsaula is an unknown quantity at this point
Ok guys how about this i embed the javascript array in the php script but how do i edit it to where it properly stores the php $memebers array values information properly in the javascript contacts array?

I put //javascript code where it is supposed to go, hope this explains it better. I am just trying to put the php array values into my javascript array after 250 runs


PHP Code:
<?
  
while(  $i 250)
        {            
            if (
$members[$i]['department'] == $depttts){
                    
//javascript code
    
contacts[j] = {"NAME""<? 
    print ($members[$i]['firstname']." ".$members[$i]['lastname']); ?>"
,"TITLE":"<? print ($members[$i]['title']); ?>","CONTACT":"<? print ($members[$i]['title']).'<br> <b>P: </b>'.($members[$i]['phone']).'<br> <b>F: </b>'.($members[$i]['fax']) ?>"}
    
            
            }        
            
            else {
             
$i++;
            }
            
$i++;
        }
        
?>
sirsaula is offline   Reply With Quote
Old 06-13-2012, 08:47 PM   PM User | #11
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
You'll have to include the whole Javascript line in the PHP print command. PHP generates your page's HTML+Javascript as its output.

Example:
Code:
print "contacts[j] = {'NAME' : '" . $members[$i]['firstname'] . " " . $members[$i]['lastname'] . "', 'TITLE' : '" . $members[$i]['title'] . "'}";
devnull69 is offline   Reply With Quote
Old 06-13-2012, 10:54 PM   PM User | #12
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
Ok, try thinking about it this way.

First the user requests a page, which goes to your server. Your server looks at the request and says "Oh, this is a PHP page! I should process the PHP". It goes through the page, looking for the PHP tags, and doing whatever is inside.

It does not know the difference between anything non-PHP. It doesn't matter if its HTML, its Javascript, its CSS. All these components are just text, they are not code.

It takes all this finally processed code and hands it to your browser, who then looks at it and interprets whether its JS, CSS, HTML, w/e

So you cannot create one loop with PHP and have it run concurrently with a JS loop. What you're doing above will simply feed out a bunch of lines, consistently overwriting contacts[j]. Not to mention, your quoting is a bit messed up, so it won't really do anything you want it to.

Tell us what you're trying to do and we can help you come up with a solution.
Keleth is offline   Reply With Quote
Old 06-18-2012, 07:58 PM   PM User | #13
sirsaula
New Coder

 
Join Date: Jun 2012
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
sirsaula is an unknown quantity at this point
Quote:
Originally Posted by devnull69 View Post
You'll have to include the whole Javascript line in the PHP print command. PHP generates your page's HTML+Javascript as its output.

Example:
Code:
print "contacts[j] = {'NAME' : '" . $members[$i]['firstname'] . " " . $members[$i]['lastname'] . "', 'TITLE' : '" . $members[$i]['title'] . "'}";
i tried your method but the code refused to execute properly
sirsaula is offline   Reply With Quote
Old 06-18-2012, 08:19 PM   PM User | #14
sirsaula
New Coder

 
Join Date: Jun 2012
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
sirsaula is an unknown quantity at this point
Quote:
Originally Posted by Keleth View Post
Ok, try thinking about it this way.

First the user requests a page, which goes to your server. Your server looks at the request and says "Oh, this is a PHP page! I should process the PHP". It goes through the page, looking for the PHP tags, and doing whatever is inside.

It does not know the difference between anything non-PHP. It doesn't matter if its HTML, its Javascript, its CSS. All these components are just text, they are not code.

It takes all this finally processed code and hands it to your browser, who then looks at it and interprets whether its JS, CSS, HTML, w/e

So you cannot create one loop with PHP and have it run concurrently with a JS loop. What you're doing above will simply feed out a bunch of lines, consistently overwriting contacts[j]. Not to mention, your quoting is a bit messed up, so it won't really do anything you want it to.

Tell us what you're trying to do and we can help you come up with a solution.
I have a multi dimensional php array that stores my members information

Now what i need to do is this

When the page is loaded it will load the php arrays members information into a table.


There is also a "live search" feature on the site whereas while user is typing in the search bar it conducts a live search and displays results to the table..

the members array should be stored in a javascript array whereas while the user is typing their search the page can search through the javascript array for any possible matches and then display it to the page.
sirsaula is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:30 PM.


Advertisement
Log in to turn off these ads.