Re: previous, current and next while looping
| From: | Analysis & Solutions | Date: | Wed, 19 Jun 2002 16:13:35 +0000 |
| Subject: | Re: previous, current and next while looping | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-102962@lists.php.net to get a copy of this message | ||
Wilbert:
On Wed, Jun 19, 2002 at 11:28:37AM +0200, Wilbert Enserink wrote:
>
> $result_ID_ID=mysql_query($gallery_list_query) OR die(mysql_error());
> $counter=0;
> while ($queryresult_IDresult=mysql_fetch_array($result_ID_ID))
> {
> $result_array['$counter']=$queryresult_IDresult;
$counter is a variable. Take $counter out of the single quotes. Thus,
you're naming the index of the array "$counter," not the integer value you
really want it to be. So, for example:
$result_array[$counter]=$queryresult_IDresult;
Do that here and everywehre in your code where you're using that variable.
But, why are you wasting time/memory reassigning it? Just assign the data
to the right place in the first place, back up in your while statement...
while ( $result_array[$counter] = mysql_fetch_array($result_ID_ID) ) {
> if ($result_array['$counter']['galleryID']==$galleryID)
> {
> $current_number=$counter+1;
> $current_item=current(($result_array));
> $next_item=next($result_array);///////this doesn't seem te work.
Are you looking for the next row from the query results? If so, you can't
get it because you haven't done a mysql_fetch_array() to get it yet. You
need to put in a call to that function here. But, do note, that will
move your record set pointer to the next row. You can back it up, if you
need to, by using mysql_data_seek().
To be honest, something looks odd with the concept behind what you're
trying to do. Exactly what are you trying to do?
> }
> $counter=$counter+1;
Simplify things:
$counter++;
> }
>
> When it has found the right record with ID number $galleryID I also
need the previous record and the next record. Anybody has any clue how to
do this?
Line wrapping in your outgoing email is a "Good Thing."
Do be warned, you won't be able to get the previous record if you're on
the first record. Your code doesn't take that into account.
If you did proper nesting, it'd be easier to read your code. Examples:
Bad:
while (...) {
blah blah
}
Good:
while (...) {
blah blah
}
Enjoy,
--Dan
--
PHP classes that make web design easier
SQL Solution | Layout Solution | Form Solution
sqlsolution.info | layoutsolution.info | formsolution.info
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409