Page 1 of 1
Repeat Region
Posted: Tue Aug 16, 2005 9:37 pm
by AliasBDI
I am looking for a PHP equivalent to the ASF function
MoveNext. My code is looping correctly but not moving to the next record each time. Rather, it shows the same record over and over. Here is my code:
Code: Select all
<?php
mysql_select_db($database_userConnection, $userConnection);
$query_modNews = "SELECT * FROM con_news ORDER BY newsDATE ASC";
$modNews = mysql_query($query_modNews, $userConnection) or die(mysql_error());
$row_modNews = mysql_fetch_assoc($modNews);
$totalRows_modNews = mysql_num_rows($modNews);
$totalROWS = 4;
$numROWS = 1;
?>
<link href="/css/main.css" rel="stylesheet" media="screen">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td><img src="/graphics/titles/areas_news.gif" width="84" height="29"></td>
</tr>
<tr>
<td bgcolor="#EEEEEE"></td>
</tr>
<?php do { ?>
<?php if ($numROWS==1) { ?>
<tr>
<td><h1><b><?php echo $row_modNews['newsTITLE']; ?></b><br><?php echo $row_modNews['newsCONTENT']; ?></h1></td>
</tr>
<?php } else { ?>
<tr>
<td bgcolor="#EEEEEE"></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" onClick="window.location.href='/news/default.php?newsID=<?php echo $row_modNews['newsID']; ?>'" onMouseOver="style.backgroundColor='#EEEEEE'; style.cursor='hand'; style.color='#FFFFFF'" onMouseOut="style.backgroundColor='#FFFFFF';"><h1><b><?php echo $row_modNews['newsTITLE']; ?></b><br><?php echo $row_modNews['newsTEASER']; ?></h1></td>
</tr>
<?php } ?>
<?php $numROWS=$numROWS+1; ?>
<?php } while ($totalROWS>=$numROWS); ?>
<tr>
<td bgcolor="#EEEEEE"></td>
</tr>
<tr>
<td><h1><b><a href="/news/">More News</a></b></h1></td>
</tr>
</table>
<?php mysql_free_result($modNews); ?>
feyd |
Posted: Tue Aug 16, 2005 9:41 pm
by feyd
your code only performs 1 mysql_fetch_assoc() ... you need to retrieve the next row during the loop somewhere...
Posted: Wed Aug 17, 2005 7:55 am
by AliasBDI
Yes. And that is what I cannot figure how to do. I assume that my query section of code is fine as well as the loop and conditionals, but I'm lacking the next record code. What would that be?
Posted: Wed Aug 17, 2005 8:10 am
by feyd
lets first figure out where you need to place it.
Do you want the cell immediately after the first one to be the next row's data? Then you put the mysql_fetch_assoc() call with your increment of $numROWS.
Do you want the cell immediately after the first one to be a repeat of the first row's data? Then you put the mysql_fetch_assoc() call inside the else block.
Other things to think about. What happens if there are less than 3 or 4 rows returned? What about 10 rows? What about no rows?
Posted: Wed Aug 17, 2005 7:49 pm
by harrisonad
The way you design your code structure is a 'hassle', because as far as I know, looping through MySQL result is just as 'easy' as this.
Code: Select all
while($data=mysql_fetch_assoc($result)){
// $data is now an array that contains the current row
// do anything with it
}
PS: no heart feelings. just doing my job.
Posted: Thu Aug 18, 2005 8:34 am
by AliasBDI
harrisonad, that was what I needed. That looped through the code and moved from record to record. However, it is only showing two records but there are more than two. What I'm aiming for is to show all records, but the first record is echoed differently (it shows more of the record's data than the others).
These are news articles. So the first article in the list needs to show the full article while the others show only the titles and teasers. This is why I created the $numROWS variable that begins at 1 and adds 1 to the number before each loop. So the code says to only show the recod with $numROWS==1 as the first record which is echoed differently.
So, the loop is moving to the next record but it is not showing all records, it is not truly looping I don't think. Here is my code now:
Code: Select all
<?php while ($row_modNews=mysql_fetch_assoc($modNews)) { ?>
<?php if ($numROWS==1) { ?>
<tr>
<td><h1><b><?php echo $row_modNews['newsTITLE']; ?></b><br><?php echo $row_modNews['newsCONTENT']; ?></h1></td>
</tr>
<?php } elseif ($numROWS>=1) { ?>
<tr>
<td bgcolor="#EEEEEE"></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" onClick="window.location.href='/news/default.php?newsID=<?php echo $row_modNews['newsID']; ?>'" onMouseOver="style.backgroundColor='#EEEEEE'; style.cursor='hand'; style.color='#FFFFFF'" onMouseOut="style.backgroundColor='#FFFFFF';"><h1><b><?php echo $row_modNews['newsTITLE']; ?></b><br><?php echo $row_modNews['newsTEASER']; ?></h1></td>
</tr>
<?php } ?>
<?php $numROWS=$numROWS+1; ?>
<?php } ?>
Posted: Thu Aug 18, 2005 8:58 am
by raghavan20
hope this works!!!
Code: Select all
<?
$userconnection = mysql_connect("HostName","UserName","Password");
mysql_select_db($database_userConnection, $userConnection);
$query_modNews = "SELECT * FROM con_news ORDER BY newsDATE ASC";
$result = mysql_query($query_modNews, $userConnection) or die(mysql_error());
$currentPosition = 0 //initially position set to zero
function moveNext($result){
if (is_resource($result)){
$i = 0;//initialize counter
while($row = mysql_fetch_row($result)){
if($i++ == $currentPosition){
$globals["currentPosition"] = $globals["currentPosition"] + 1;
return $row;
}
}
}
}
//use movenext($result) to retrieve subsequent records
//use row, array and loop: row[0], row[1]...count($row)
?>
Posted: Thu Aug 18, 2005 1:57 pm
by AliasBDI
raghavan, too complex for me. I'm not familiar with functions yet. I was just looking for a remedy to the code I have. Thanks though. I'll get to your geniousness soon ... I'm a slow learner.
Posted: Thu Aug 18, 2005 2:06 pm
by raghavan20
me too, not a great programmer
you just have to use the function.
use mysql_query($query)//any query to pass the result to the function and function has a global variable as the record pointer and it returns the record pointed by the currentPointer and increments itself.
There are actually database classes which do it in a different way.
The DB class would have a pointer that stores the current pointer index.
Posted: Thu Aug 18, 2005 2:49 pm
by AliasBDI
Ah ha. I got somewhere. It looks like it is working fine except that it is not showing the first record! Any ideas as to why?
Posted: Thu Aug 18, 2005 3:09 pm
by raghavan20
Code: Select all
<?
$userconnection = mysql_connect("HostName","UserName","Password");
mysql_select_db($database_userConnection, $userConnection);
$query_modNews = "SELECT * FROM con_news ORDER BY newsDATE ASC";
$result = mysql_query($query_modNews, $userConnection) or die(mysql_error());
$currentPosition = 0 //initially position set to zero
function moveNext($result){
if (is_resource($result)){
$i = 0;//initialize counter
echo "current global pointer position:" .$globals["currentPosition"]."<br />";
while($row = mysql_fetch_row($result)){
if($i++ == $globals["currentPosition"]){
$globals["currentPosition"] = $globals["currentPosition"] + 1;
return $row;
}
}
}
}
//use movenext($result) to retrieve subsequent records
//use row, array
try it now and have a look at the pointer position printed on the screen. see whether the first printed out value is zero.
If you want to use this function everywhere in any file, just copy the function into a new php file and include in all other php files and replace the global variable with a session variable.

Posted: Thu Aug 18, 2005 7:40 pm
by AliasBDI
Anyone know how to remedy this, besides a function (sorry
raghavan20, just don't want to go there yet)? The loop works but it does not echo the very first record. Here is the code once again...
Code: Select all
<?php
mysql_select_db($database_userConnection, $userConnection);
$query_modNews = "SELECT * FROM con_news ORDER BY newsDATE DESC LIMIT 3";
$modNews = mysql_query($query_modNews, $userConnection) or die(mysql_error());
$row_modNews = mysql_fetch_assoc($modNews);
$totalRows_modNews = mysql_num_rows($modNews);
$numROWS = 1;
?>
<link href="/css/main.css" rel="stylesheet" media="screen">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td><img src="/graphics/titles/areas_news.gif" width="84" height="29"></td>
</tr>
<tr>
<td bgcolor="#EEEEEE"></td>
</tr>
<?php while ($row_modNews=mysql_fetch_assoc($modNews)) { ?>
<?php if ($numROWS==1) { ?>
<tr>
<td><h1><b><?php echo $row_modNews['newsTITLE']; ?></b><br><?php echo $row_modNews['newsCONTENT']; ?></h1></td>
</tr>
<?php } else { ?>
<tr>
<td bgcolor="#EEEEEE"></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" onClick="window.location.href='/news/default.php?newsID=<?php echo $row_modNews['newsID']; ?>'" onMouseOver="style.backgroundColor='#EEEEEE'; style.cursor='hand'; style.color='#FFFFFF'" onMouseOut="style.backgroundColor='#FFFFFF';"><h1><b><?php echo $row_modNews['newsTITLE']; ?></b><br><?php echo $row_modNews['newsTEASER']; ?></h1></td>
</tr>
<?php } ?>
<?php $numROWS=$numROWS+1; ?>
<?php } ?>
<tr>
<td bgcolor="#EEEEEE"></td>
</tr>
<tr>
<td><h1><b><a href="/news/">More News</a></b></h1></td>
</tr>
</table>
<?php mysql_free_result($modNews); ?>
Posted: Thu Aug 18, 2005 7:41 pm
by feyd
remove the mysql_fetch_assoc() call immediately after the query call.
Code: Select all
$row_modNews = mysql_fetch_assoc($modNews);
Posted: Thu Aug 18, 2005 7:46 pm
by AliasBDI
feyd, my hero. Thanks, that solved the problem. *big applaud for feyd*