Jump to content

use the data from a while loop twice in a page?


cdoyle

Recommended Posts

Hi,

 

I'm not sure if this is possible, but can you use the values from a while loop multiple times within a page?

I'm not sure if I'm explaining it right, so I'll try and show what I mean below.

Say you have a while loop on a page that pulls data.


$query 1 = $db->execute ("Select `column1`, `column2` FROM table);
$results1 = $query1 ->fetchrow();
while
{
looped results here and do something with them.
}

 

but let's say I need to use the exact same data again later on in the page.  Do I need to recreate the exact same query again,  or is there a way to re-loop it?

 


$query 1 = $db->execute ("Select `column1`, `column2` FROM table);
$results1 = $query1 ->fetchrow();
while
{
looped results here and do something with them.
}

if (something happens)
{
need to use the data again from the loop above.
}
else
{
don't need it
}

 

So in the if Statement,  I need that data again. 

I thought maybe I could do something like

$results2 = $query1 ->fetchrow();

and that would reloop it based off of $query  but it doesn't do anything.

 

I could just create another query, that's exactly the same as the first one,  but thought it would be nice if I could just use the first one again if possible.

 

Thanks,

Link to comment
Share on other sites

OK,

I'm reading up on multi dimensional arrays, and trying to figure them out.  All the tutorials I see are for manually creating an array.

I'm getting confused on how to assign the values from the loop to an array.

 

 

 

 

 

Link to comment
Share on other sites

If you are using mysql - http://us3.php.net/mysql_data_seek

 

The db class you implied probably has a method that performs the same function or could be extended to add and/or it may in fact already internally hold the results in an intermediate array that you can just reuse.

I'm using ADOdb,  reading that page I'm still unsure how to do what I need to do.

Link to comment
Share on other sites

I'm still not sure what I'm doing,  I'm looking over that page and it's all confusing.

 

I found this example

<?

// Start snipit 1

$sql = "SELECT * from <table>";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
    // do stuff with $row
}

mysql_data_seek($result, 0);

while ($row = mysql_fetch_assoc($result)) {
    // do other stuff with $row
}

 

I thought this is what I would want, but I get these errors when I try them

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/caraudi/public_html/CAC_Mafia_Test_Site/test.php on line 31

 

Warning: mysql_data_seek(): supplied argument is not a valid MySQL result resource in /home/caraudi/public_html/CAC_Mafia_Test_Site/test.php on line 35

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/caraudi/public_html/CAC_Mafia_Test_Site/test.php on line 37

 

 

Would it be possible to post a sample code, that I could take a look at?

Link to comment
Share on other sites

Those errors (please read them) mean that the result resource you are using in the function calls in those lines of code is not valid. This is because the query failed and returned a FALSE value instead of a result resource.

 

If this is your actual query -

 

$sql = "SELECT * from <table>";

 

It failed because the characters '<' and '>' are not correct sql syntax.

Link to comment
Share on other sites

also 2x check that in your production code you don't do something like

 

<?

// Start snipit 1

$sql = "SELECT * from <table>";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
    // do stuff with $row
}
mysql_free_result($result);  // too early...

mysql_data_seek($result, 0);  // BOOM!

while ($row = mysql_fetch_assoc($result)) {
    // do other stuff with $row
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.