0

I have a while loop that assigns user ID's to a variable. The variable is an array. When I assign the variable to another in a link like this: it returns proper ID's on the click but only when the link is in a while loop as well. How ( is it possible ) to place the link outside the while loop and get the same ID data the variable holds ?

This code works:

while ( $row = mysqli_fetch_array($sql)) { 
    $variable = $row['user_id'];
    echo "<a href='index.php?var=$variable'></a>";
} 

This one doesn't in this case:

PHP:

while ( $row = mysqli_fetch_array($sql)) { 
    $variable[] .= $row['user_id'];
} 

HTML:

for ($i = 0 ; $i <100 ; $i++ );
<a href='index.php?var=$variable[$i]'></a>

Thanks for comments..

1
  • You have to print the value inside the HTML, like so: <a href='index.php?var=<?=$variable[$i]?>'></a> Commented Sep 30, 2011 at 20:22

3 Answers 3

5

You've got a syntax goof:

for ($i = 0 ; $i <100 ; $i++ );
                              ^----

The semicolon terminates the for loop, so you're doing an empty loop. Change it to:

for ($i = 0 ; $i <100 ; $i++ )
    echo "<a href......etc....";

or better yet:

for ($i = 0 ; $i <100 ; $i++ ) {
    echo "<a href......etc....";
}
Sign up to request clarification or add additional context in comments.

1 Comment

And also PHP does not evaluate simple quote strings '' only the double ones ""
1
while ( $row = mysqli_fetch_array($sql)) { 
    $variable[] .= $row['user_id']; //Wrong
    $variable[] = $row['user_id']; //Correct
}

foreach($variable as $value) {
    echo "<a href='index.php?var=$value'></a>"; // Be sure to use double quotes
}

2 Comments

I'll try it out in a sec - yes I always use double quotes after echo / print - I didn't write them in my question...
Thanks ! Works... By the way you meant: index.php?var=$value ( not $val ) I guess...
0

Several issues are coming into play here:

  1. $variable is defined within the scope of the while loop, so it is inaccessible outside of said loop (though PHP may let you get away with this one).
  2. You are assuming that there are precisely 100 rows (indexed 0-99) returned by performing the SQL query represented by $sql. While this may be true, it's not good practice to assume this, and you should handle however many rows are actually returned by performing that query.
  3. Perhaps most importantly, if you are truly using <a href='index.php?var=$variable[$i]'></a> in an HTML context, it will not work, as $variable[$i] is PHP code. You will need to put this in a PHP document, somewhere between the <?php and ?> tags.

3 Comments

#1 is outright false. php's variable scoping is at the function/method level. A var defined inside a while/for/if block is available outside the block as well.
Hey - of course I have all the html code within PHP ( to be concise I skipped the rest of it ) . I also have $i < $row_number; again, I wrote 100 to be more concise...
@MarcB Sure...they are available but strangely I managed to get ID loops in another program with the $variable outside the loop. In this case it fails...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.