Jump to content


Photo

Using Query Results More Than Once


  • Please log in to reply
9 replies to this topic

#1 mouli

mouli
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 29 June 2006 - 12:45 AM

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:
<?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));

This works well.
Next I tabulate the results using another function that uses the same query results using:
$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'];

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

#2 yong

yong
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 29 June 2006 - 03:27 AM

" 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"



#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 29 June 2006 - 03:33 AM



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'];
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 mouli

mouli
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 29 June 2006 - 04:21 AM

[!--quoteo(post=389091:date=Jun 29 2006, 03:33 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Jun 29 2006, 03:33 PM) View Post[/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

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 29 June 2006 - 04:24 AM

[!--quoteo(post=389096:date=Jun 29 2006, 04:21 AM:name=mouli)--][div class=\'quotetop\']QUOTE(mouli @ Jun 29 2006, 04:21 AM) View Post[/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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 29 June 2006 - 05:42 AM

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

then you can access the information in the $list array all day long, by looping through it, or using it directly. for example:
//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'];

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 29 June 2006 - 05:52 AM

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) ) {

}
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 29 June 2006 - 06:12 AM

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.
$sql = "select * from artists";
$artistlist = mysql_query($sql);

while ($row_artistlist = mysql_fetch_assoc($artistlist)) {
   $list[] = $row_artistlist;
}
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:
$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');
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:
foreach ($list as $key => $val) {
   echo $key . " : " . $val . "<br>";
}
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).
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 29 June 2006 - 06:26 AM

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) View Post[/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
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 29 June 2006 - 06:29 AM

yes. this would be considered a multi-dimensional array.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users