PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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:
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?
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?
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:
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.
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.
<?
$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.
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...