PDA

View Full Version : Determine if variable is last in a while statement [+6rep]



Blinger1
10-07-2009, 10:24 AM
G'day y'all!

Quick question, how do i determine if a variable is the last in a while statement? Say the following is my while statement, i don't want there to be the "<hr>" after the last entry..


while($row = mysql_fetch_array($query)){
$id = $row['id'];

$from = $row['from'];
$from = username($from);

$message = $row['message'];
$date = $row['date'];

echo("<p><strong>id</strong>: $id<br />
<strong>from</strong>: $from<br />
<strong>message</strong>: $message<br />
<strong>date</strong>: $date<br />
<hr>
");
}

No need to comment on the code apart from how to determine the last variable plz.

Excellent2
10-07-2009, 10:27 AM
I'm not sure about this but couldn't you use strpos()?

BoyBetterKnow
10-07-2009, 10:34 AM
Just do a mysql_num_rows and then in the while increase a var by one each loop. Then if the mysql_num_rows resource = the number it's currently on, don't echo the <hr>

Blinger1
10-07-2009, 10:50 AM
Just do a mysql_num_rows and then in the while increase a var by one each loop. Then if the mysql_num_rows resource = the number it's currently on, don't echo the <hr>
I used your method. cheers!

and i +repped you both :)

BoyBetterKnow
10-07-2009, 11:24 AM
I used your method. cheers!

and i +repped you both :)

Thanks very much. I need rep haha.

Blinger1
10-07-2009, 11:29 AM
hmm.. it looks like it?



$num_rows = mysql_num_rows($query)
$whileCount = 0;
while($row = mysql_fetch_array($query)){
$whilecount ++;
$id = $row['id'];

$from = $row['from'];
$from = username($from);

$message = $row['message'];
$date = $row['date'];

echo("<p><strong>id</strong>: $id<br />
<strong>from</strong>: $from<br />
<strong>message</strong>: $message<br />
<strong>date</strong>: $date<br />
");
if($num_rows != $whileCount){
echo("<hr>");
}
}


was my end code btw ;)

Dentafrice
10-07-2009, 03:46 PM
If you want an easier way; without incrementing, checking the numbers, or a wasteful
num_rows :P

It puts the <hr> on all of them.. then just removes the last one and echos out the combined string.



while($row = mysql_fetch_array($query)) {
$string .= "<p><strong>id</strong>: {$row["id"]}<br />";

$from = username($row["from"]);
$string .= "<strong>from</strong>: {$from}<br />";

$string .= "<strong>message</strong>: {$row["message"]}<br />";
$string .= "<strong>date</strong>: {$row["date"]}<br />";
$string .= "</p><hr />";
}

echo substr_replace($string, "", -6);

Blinger1
11-07-2009, 09:27 AM
Your code looks more complicated!

So i have a few questions: what does the ".=" thing mean? I presume it means $string .= $string + "</p><hr />"; ?

And also, what does the last line mean? That is the most confusing part!

Tomm
11-07-2009, 09:45 AM
It just removes the last 6 characters from $string, not anything super complicated :P


Your code looks more complicated!

So i have a few questions: what does the ".=" thing mean? I presume it means $string .= $string+"</p><hr />"; ?

And also, what does the last line mean? That is the most confusing part!

Blinger1
11-07-2009, 09:48 AM
oh right! +rep to you too then :)

Jam-ez
11-07-2009, 09:49 AM
Your code looks more complicated!

So i have a few questions: what does the ".=" thing mean? I presume it means $string .= $string + "</p><hr />"; ?

And also, what does the last line mean? That is the most confusing part!

.=

$variable = 'hi';
echo $variable; // returns hi
$variable .= ", what's your name?";
echo $variable // returns hi, what's your name?
// All in all, when reading this in your browser, the return would be: hihi, what's your name?
/////////////////////////////////////////////
// However, if we use the code below:
$variable = 'hi';
$variable .= ", what's your name?";
echo $variable // returns hi, what's your name?
// The end result is as expected hi, what's your name?

As you can probably see, .= just adds to the existing variable.

As for the last line "substr_replace" simply replaces the text in a certain part of the string. (http://uk3.php.net/substr_replace)

Edit: Oh yeah, it means what you said in your post, pft what a pointless explanation.

Blob
11-07-2009, 08:30 PM
.=

$variable = 'hi';
echo $variable; // returns hi
$variable .= ", what's your name?";
echo $variable // returns hi, what's your name?
// All in all, when reading this in your browser, the return would be: hihi, what's your name?
/////////////////////////////////////////////
// However, if we use the code below:
$variable = 'hi';
$variable .= ", what's your name?";
echo $variable // returns hi, what's your name?
// The end result is as expected hi, what's your name?

As you can probably see, .= just adds to the existing variable.

As for the last line "substr_replace" simply replaces the text in a certain part of the string. (http://uk3.php.net/substr_replace)

Edit: Oh yeah, it means what you said in your post, pft what a pointless explanation.

Actually the end result will be an error due to missing ;'s

Jam-ez
11-07-2009, 09:06 PM
Actually the end result will be an error due to missing ;'s

Ah, you win. :)

Blob
11-07-2009, 09:11 PM
Ah, you win. :)

:P

Good example, though.

Jam-ez
11-07-2009, 10:41 PM
:P

Good example, though.

Thanks a bunch, I don't post much round here (especially considering how long I've been on here...) so I try to make my helpful posts helpful. :)

Want to hide these adverts? Register an account for free!