0

I can loop through a table, grab the rows and echo them out within the loop. How do I use this data outside the loop?

$query=("SELECT * FROM poplinks");
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){
    $popid = $row['popid'];
    $popname = $row['popname'];
    $popurl = $row['popurl'];

echo $popurl;
}

The echo statement above echos the 10 popurl values in the db, but if I try and echo $popurl outside of the loop I only get the last value. I guess I want an array of all the values stored in a variable.

Update: The data is going into something like the below HTML: (note "echo $popurl" on line 3. The idea is to get the Twitter Bootstrap Typeahead function to read from a database. At the moment by appending [] to each of the variables inside the loop, I can get the below code working if I include the index e.g. but I can't get the entire row in there.

<div class="well">
<form action="" method="post" onsubmit="proceed();">
<input type="text" class="span3" name="content" value="<?php echo $liLink->sContent; ?>" data-provide="typeahead" data-items="4" data-source='["Alabama", "<?php echo $popurl ?>" ,"Alaska","Arizona","Arkansas"]'/>
<input type="submit" name="submit" value="save" />
</form>

1
  • As pointed out by @Abid Hussain, store the value of popurl in an array so that on every loop, the value gets appended and not overwritten. Commented Aug 1, 2012 at 5:06

1 Answer 1

4

Try this

$query=("SELECT * FROM poplinks");
$result = mysql_query($query);

$myArray='';
while($row = mysql_fetch_array($result)){
    $popid = $row['popid'];
    $popname = $row['popname'];
    $popurl = $row['popurl'];

$myArray[] = $popurl;
}

echo "<pre>";
print_r($myArray);
Sign up to request clarification or add additional context in comments.

Comments

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.