Page 1 of 1
reset mysql_fetch_row()
Posted: Tue Nov 25, 2003 3:46 am
by Castles
Hi, I have a for loop that uses the mysql fetch row function... My problem is I want to use it for a second loop after I have finished the first loop.. eg ...
for($i = 0; $i < mysql_num_rows($qr); $i++) {
$theme = mysql_fetch_row($qr);
}
for($i = 0; $i < mysql_num_rows($qr); $i++) {
$theme = mysql_fetch_row($qr);
}
when I run it the 2nd part doesn't fetch anything because it is all fetched in the first part.. is there a way to reset the fetch status to 0?
Posted: Tue Nov 25, 2003 3:50 am
by infolock
is $theme suposed to be an array? because from what i can see with this loop, it's only going to keep the very last record it finds and store it to $theme.. so the loop is useless unless it has other actions than what you posted.
edit: and why would you run this again? it's already stored the data into the variable, so all you would have to do is call it when you need it..
edit : a better way to write this loop would be
Code: Select all
<?php
$theme = array();
for($i = 0; $i < mysql_num_rows($qr); $i++)
{
$theme[] = mysql_fetch_row($qr); // this stores each record to a field within the array
}
// call it like this
echo $theme[0];
echo '<br />';
echo $theme[1];
...
// .... or call it like this
for($i=0; $i<mysql_num_rows($qr); $i++)
{
echo $theme[$i];
echo '<br />';
}
?>
Posted: Tue Nov 25, 2003 4:01 am
by Castles
the actual code is this..
Code: Select all
<?php
// create an empty array to store years
$years = array();
// add to array if flag is true
$flag = true;
// add values to list menu
for($i = 0; $i < mysql_num_rows($qr); $i++) {
$theme = mysql_fetch_row($qr);
// loop through array and see if values already exist
for($j = 0; $j < count($years); $j++) {
if ($years[$j] == substr($theme[1], 0, 4)) {
$flag = false;
}
}
// if value needs to be added to array
if ($flag == true) {
array_push($years, substr($theme[1], 0, 4));
}
// reset flag
$flag = true;
}
// add array to page
for($i = 0; $i < count($years); $i++) {
echo "<option value="" . $years[$i] . """;
//select year that was selected
if ($_SESSION['session_year'] == $years[$i]) {
echo "selected";
//set session to current selection
$_SESSION['session_year'] = $years[$i];
}
//select current year on first visit to page
if (!$_SESSION['session_year'] && $years[$i] == date("Y")) {
echo "selected";
//set session to current selection
$_SESSION['session_year'] = date("Y");
}
echo ">" . $years[$i] . "</option>";
}
echo "</select></td><td><select name='list_month' class='textBox' id='list_month'>";
// select all themes
$qr = mysql_query("Select ID, date from tbl_theme ORDER BY date") or die("Could not query: " . mysql_error());
// add date values
// create an empty array to store months
$months = array();
// add to array if flag is true
$flag = true;
// add values to list menu
for($i = 0; $i < mysql_num_rows($qr); $i++) {
$theme = mysql_fetch_row($qr);
//only loop through themes that are of the session year
if (substr($theme[1], 0, 4) == $_SESSION['session_year']) {
// loop through array and see if values already exist
for($j = 0; $j < count($months); $j++) {
if ($months[$j] == substr($theme[1], 5, 2)) {
$flag = false;
}
}
// if value needs to be added to array
if ($flag == true) {
array_push($months, substr($theme[1], 5, 2));
}
// reset flag
$flag = true;
}
}
// add array to page
for($i = 0; $i < count($months); $i++) {
echo "<option value="" . $months[$i] . """;
if ($months[$i] == date("n")) {
echo "selected";
}
//
echo ">" . date("F", mktime(0, 0, 0, $months[$i], 1, 0000)) . "</option>";
}
// close connection
mysql_close($con);
?>
Posted: Tue Nov 25, 2003 4:26 am
by infolock
sorry for my misunderstanding of what you are wanting... what i guess you should do ( if you just want theme to be a variable instead of an array), is to declare it without a for loop..
like this :
Code: Select all
<?php
<?php
// create an empty array to store years
$years = array();
$theme = mysql_fetch_row($qr);
// add to array if flag is true
$flag = true;
// add values to list menu
// loop through array and see if values already exist
for($j = 0; $j < count($years); $j++)
{
if ($years[$j] == substr($theme[1], 0, 4))
{
$flag = false;
}
// if value needs to be added to array
if ($flag == true)
{
array_push($years, substr($theme[1], 0, 4));
}
// reset flag
$flag = true;
}
// add array to page
for($i = 0; $i < count($years); $i++)
{
echo "<option value="" . $years[$i] . """;
//select year that was selected
if ($_SESSION['session_year'] == $years[$i])
{
echo "selected";
//set session to current selection
$_SESSION['session_year'] = $years[$i];
}
//select current year on first visit to page
if (!$_SESSION['session_year'] && $years[$i] == date("Y"))
{
echo "selected";
//set session to current selection
$_SESSION['session_year'] = date("Y");
}
echo ">" . $years[$i] . "</option>";
}
echo "</select></td><td><select name='list_month' class='textBox' id='list_month'>";
// select all themes
$qr = mysql_query("Select ID, date from tbl_theme ORDER BY date") or die("Could not query: " . mysql_error());
// add date values
// create an empty array to store months
$months = array();
// add to array if flag is true
$flag = true;
// add values to list menu
/*
// don't need this now
for($i = 0; $i < mysql_num_rows($qr); $i++)
{
// $theme = mysql_fetch_row($qr);
*/
//only loop through themes that are of the session year
if (substr($theme[1], 0, 4) == $_SESSION['session_year'])
{
// loop through array and see if values already exist
for($j = 0; $j < count($months); $j++)
{
if ($months[$j] == substr($theme[1], 5, 2))
{
$flag = false;
}
}
// if value needs to be added to array
if ($flag == true)
{
array_push($months, substr($theme[1], 5, 2));
}
// reset flag
$flag = true;
}
// add array to page
for($i = 0; $i < count($months); $i++)
{
echo "<option value="" . $months[$i] . """;
if ($months[$i] == date("n"))
{
echo "selected";
}
//
echo ">" . date("F", mktime(0, 0, 0, $months[$i], 1, 0000)) . "</option>";
}
// close connection
mysql_close($con);
?>
Posted: Tue Nov 25, 2003 7:45 am
by Wayne
will reset the query results back to the beginning.
Posted: Tue Nov 25, 2003 4:01 pm
by Castles
Thanks for all your help, I ended up using mysql_data_seek($qr,0) to reset the fetch... I had to keep the array row fetch in the code for each loop because it loops through everything in the database twice (adds to combo boxes). thanks