Jump to content

Archived

This topic is now archived and is closed to further replies.

mouli

Using Query Results More Than Once

Recommended Posts

I have a page that queries a database, the results being stored in the variable $artistlist.
I then use these results to list the artists as follows:
[code]
<?php do { ?>
<tr>
<td><?php
echo "<p align=\"right\">". $row_artistlist['artist_firstname']." <span class=\"emphasis\">". $row_artistlist['artist_lastname']."</span></p> ";
?>
</td>
<td> </td>
</tr>
<tr>
<td><img src="spacer.gif" width="1" height="5"></td>
<td></td>
</tr>
<?php } while ($row_artistlist = mysql_fetch_assoc($artistlist));
[/code]

This works well.
Next I tabulate the results using another function that uses the same query results using:
[code]
$row = mysql_fetch_assoc($artistlist);
            $cell_gallery=$row['gallery'];
            $cell_artist_firstname=ucfirst($row['artist_firstname']);
            $cell_artist_lastname=ucfirst($row['artist_lastname']);
            $cell_position=$row['position'];
[/code]

But this second use of $artistlist gives empty results ie $cell_gallery etc are null.

I can use one or the other but I cant get both useages of $artistlist to work on the page.
I hope that makes sense.
Why can I not access the results array twice in the same page??

Many thanks

mouli

Share this post


Link to post
Share on other sites
" Why can I not access the results array twice in the same page??"

you cant access the results array twice in the same page because the "key" i dot know i say so is nicety or not

in the memory it's has a key to point to the results and if you read one she will move next read one ,move next like this until the last one

so if you read one you will not read that again unless you control the "key"

Share this post


Link to post
Share on other sites


As exsplained as above the varable set to $row reads the last varable to $row.


$varable_chaned= mysql_fetch_assoc($artistlist);
$cell_gallery=$varable_chaned['gallery'];
$cell_artist_firstname=ucfirst($varable_chaned['artist_firstname']);
$cell_artist_lastname=ucfirst($varable_chaned['artist_lastname']);
$cell_position=$varable_chaned['position'];

Share this post


Link to post
Share on other sites
[!--quoteo(post=389091:date=Jun 29 2006, 03:33 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Jun 29 2006, 03:33 PM) [snapback]389091[/snapback][/div][div class=\'quotemain\'][!--quotec--]
As exsplained as above the varable set to $row reads the last varable to $row.
$varable_chaned= mysql_fetch_assoc($artistlist);
$cell_gallery=$varable_chaned['gallery'];
$cell_artist_firstname=ucfirst($varable_chaned['artist_firstname']);
$cell_artist_lastname=ucfirst($varable_chaned['artist_lastname']);
$cell_position=$varable_chaned['position'];
[/quote]
Many thanks redarrow and yong.
That makes sense. As I understand it the first time read the results the pointer ends up at the end so without reseting the pointer the next time I read the results I will get nothing. Even changing the variable doesn't work. I had to use a new query, then it worked.
Thanks again.

mouli

Share this post


Link to post
Share on other sites
[!--quoteo(post=389096:date=Jun 29 2006, 04:21 AM:name=mouli)--][div class=\'quotetop\']QUOTE(mouli @ Jun 29 2006, 04:21 AM) [snapback]389096[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Many thanks redarrow and yong.
That makes sense. As I understand it the first time read the results the pointer ends up at the end so without reseting the pointer the next time I read the results I will get nothing. Even changing the variable doesn't work. I had to use a new query, then it worked.
Thanks again.

mouli
[/quote]

Sorry should of exsplained that ok.

Share this post


Link to post
Share on other sites
instead of doing a query twice, just assign the fetched information to a new array. example:
[code]
while ($row_artistlist = mysql_fetch_assoc($artistlist)) {
   $list[] = $row_artistlist;
}
[/code]

then you can access the information in the $list array all day long, by looping through it, or using it directly. for example:
[code]
//does a loop that echoes out all the info
foreach ($list as $key => $val) {
   echo $key . " : " . $val . "<br>";
}

//echo out the 3rd artist's firstname in the list.
echo $list[2]['artist_firstname'];
[/code]

Share this post


Link to post
Share on other sites
Crayon Violent i learn a lot from you and cheers your a star.

can you brake the above code down so i can properly study it cheers.

1 min issint this the same

while (list ($val,$key)=each($what_ever) ) {

}

Share this post


Link to post
Share on other sites
let's say we have in a table called artists, the artist's first and last name:

artists
====
artist_firstname | artist_lastname
--------------------------------------
bob | marley
marylin | manson
boy | george

and let's say we want to just get all the information from the table and store it in an array and be able to print it.
[code]
$sql = "select * from artists";
$artistlist = mysql_query($sql);

while ($row_artistlist = mysql_fetch_assoc($artistlist)) {
   $list[] = $row_artistlist;
}
[/code]
so we have the query string ($sql) and we run the query and store the results in $artistlist. then we do a loop to retrieve all the rows in the result source ($artistlist) and store each row in a new position in our $list array.

doing this loop is the equivelent of doing this:
[code]
$list[0] = array('artist_firstname' => 'bob', 'artist_lastname' => 'marley');
$list[1] = array('artist_firstname' => 'marylin', 'artist_lastname' => 'manson');
$list[2] = array('artist_firstname' => 'boy', 'artist_lastname' => 'george');
[/code]
so now, depending on how we want to access this information, we can do a loop to dump it all out, or just access it directly. one loop example is the foreach loop i mentioned above:
[code]
foreach ($list as $key => $val) {
   echo $key . " : " . $val . "<br>";
}
[/code]
basically this says this: for each element in the $list array (that is, for $list[0], $list[1], $list[2], we will assign the key ('artist_firstname' and 'artist_lastname') to $key and the value for that key (example: in $list[0], the values are bob and marley), and we will echo out $key and $val.

so that foreach loop will output:

bob marley
marylin manson
boy george

most scripts will involve dumping out all of the results in a loop in such a fashion. if you didn't want to do that, then chances are, you wouldn't have done that specific query to begin with. However, you can access individual values by explicitly calling them. For example:

echo $list[1]['artist_firstname'] . " " . $list[2]['artist_lastname'];

will echo out

marylin george

(notice i echoed the firstname from element 1 and the lastname from element 2).

Share this post


Link to post
Share on other sites
Thank you so much you exsplain it so clair and in a book 30 pages lol...........

cheers.

[!--quoteo(post=389136:date=Jun 29 2006, 06:20 AM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Jun 29 2006, 06:20 AM) [snapback]389136[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Thank you so much you exsplain it so clair and in a book 30 pages lol...........

cheers.
[/quote]
is this a array of an array

multi dimonanal array

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.