Jump to content

Recommended Posts

hi, i dont know much about using loops.. i know a little about while loops so i tried using one for trying to display how many pages of message you have in your inbox.. (this froze my computer)

 

heres some of my code

echo "<form METHOD=get><input type=hidden name=view value=Message><table width=70% border=0 cellspacing=1 cellpadding=0>
<tr><td background=images/forum_board.jpg height=25 width=0></td><td background=images/forum_board.jpg height=25 width=33%><b><center>Subject</center></b></td><td background=images/forum_board.jpg height=25 width=33%><b><center>From</center></b></td><td background=images/forum_board.jpg height=25 width=33%><b><center>Date</center></td></tr>";
$slimit = $action*15;
$llimit = ($action+1)*15;
$pms = mysql_query("SELECT * FROM pm WHERE pm_to='$get_user[id]' ORDER BY id DESC LIMIT $slimit, $llimit");
while ($message = mysql_fetch_array($pms)) {
$from = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE id='$message[pm_from]'"));
echo "<tr><td bgcolor=#111111 height=25 width=0><input type=radio name=action value=$message[id] /></td><td bgcolor=#111111 width=33%>- <a href='?page=MessageCenter&view=ViewMessage&action=$message[id]' title='$message[text]'>$message[subject]</a></td><td width=33% bgcolor=#111111><a href=?page=Profile&view=$from[id]> $from[username]</a> [$from[id]]</td><td bgcolor=#111111 width=33%> $message[date]</td></tr>";}
echo "</table><table width=70%><tr><td><div align=left><img src=images/arrows_up.png> <input type=submit name=page value=Delete class=form size=5> <input type=submit name=page value=Reply class=form size=5></td></tr></table></form>";

//the code i need help with
$pmrows = mysql_num_rows(mysql_query("SELECT * FROM pm WHERE pm_to='$get_user[id]'"));
while($pmrows>$llimit){
$i++;
$p=$l-1;
echo "<a href=?page=MessageCenter&action=$p>$i</a>"; }

Link to comment
https://forums.phpfreaks.com/topic/50790-using-loops/
Share on other sites

this froze my computer

 

You prbably had an infinant loop somewhere, code like this...

 

$pmrows = mysql_num_rows(mysql_query("SELECT * FROM pm WHERE pm_to='$get_user[id]'"));

 

Is terrible, you never check if your query succeeds, but this line here....

 

while($pmrows>$llimit){

 

You never increment $pmrows, so this loop will go forever, or until your cpu is fried.

Link to comment
https://forums.phpfreaks.com/topic/50790-using-loops/#findComment-249748
Share on other sites

Look for a tutorial on using mysql's JOIN. You should be able to get what you want in one query. executing queries within loops is VERY inficient.

 

Besides that, instead of calling mysql_* fuctions within each other, sperate them and check for errors. eg;

 

<?php

  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      // it is now safe to fetch data.
      while($row = mysql_fetch_assoc($result)) {
        echo $row['foo'];
      }
    } else {
      // no record found.
  } else {
    // query failed.
  }

?>

 

Allot more code, but allot easier to debug, and wont generate errorrs if something goes wrong.

 

PS: Notice the indentation? This makes your code more readable. The reason Im not using any of your code as an example is simply because I cannot be bothered trying to read it.

Link to comment
https://forums.phpfreaks.com/topic/50790-using-loops/#findComment-249758
Share on other sites

ill put it in php tags... the mysql isnt my porblem... im not sure what loop i should be using and how i should be using it

 

echo "<form METHOD=get><input type=hidden name=view value=Message><table width=70% border=0 cellspacing=1 cellpadding=0>
<tr><td background=images/forum_board.jpg height=25 width=0></td><td background=images/forum_board.jpg height=25 width=33%><b><center>Subject</center></b></td><td background=images/forum_board.jpg height=25 width=33%><b><center>From</center></b></td><td background=images/forum_board.jpg height=25 width=33%><b><center>Date</center></td></tr>";
$slimit = $action*15;
$llimit = ($action+1)*15;
$pms = mysql_query("SELECT * FROM pm WHERE pm_to='$get_user[id]' ORDER BY id DESC LIMIT $slimit, $llimit");
while ($message = mysql_fetch_array($pms)) {
$from = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE id='$message[pm_from]'"));
echo "<tr><td bgcolor=#111111 height=25 width=0><input type=radio name=action value=$message[id] /></td><td bgcolor=#111111 width=33%>- <a href='?page=MessageCenter&view=ViewMessage&action=$message[id]' title='$message[text]'>$message[subject]</a></td><td width=33% bgcolor=#111111><a href=?page=Profile&view=$from[id]> $from[username]</a> [$from[id]]</td><td bgcolor=#111111 width=33%> $message[date]</td></tr>";}
echo "</table><table width=70%><tr><td><div align=left><img src=images/arrows_up.png> <input type=submit name=page value=Delete class=form size=5> <input type=submit name=page value=Reply class=form size=5></td></tr></table></form>";

//the code i need help with
$pmrows = mysql_num_rows(mysql_query("SELECT * FROM pm WHERE pm_to='$get_user[id]'"));
while($pmrows>$llimit){
$i++;
$p=$l-1;
echo "<a href=?page=MessageCenter&action=$p>$i</a>"; }

 

you may notice the reason it looks messy is beacuase its just a big echo...

Link to comment
https://forums.phpfreaks.com/topic/50790-using-loops/#findComment-250062
Share on other sites

A couple tips.

 

1) indent code to make it easier to read, I just got into this habit about a month or 2 ago.... much easier to debug and find problems.

 

if($this > $that)
{
     while(1 < 2)
     {
         echo 'do something here';
     }
}

 

is a lot easier to read and decipher, than

if($this > $that){while(1 < 2){echo 'do something here';}}

 

 

2) There is no need to echo large ass chunks of pure HTML code. Drop in and out of php as you need. e.g.

 

I started to do some indenting to make it easier to read, but that will take a while...but here is an incomplete example of what I mean. Notice the shorthand echo statement.

<?= ?>

Very handy when you have a couple php vars scattered throught a large chunk of HTML

 

<form METHOD=get>
<input type=hidden name=view value=Message>
<table width=70% border=0 cellspacing=1 cellpadding=0>
	<tr>
		<td background=images/forum_board.jpg height=25 width=0>
		</td>
		<td background=images/forum_board.jpg height=25 width=33%>
			<b>
				<center>Subject</center>
			</b>
		</td>
<td background=images/forum_board.jpg height=25 width=33%>
<b><center>From</center></b></td>	
<td background=images/forum_board.jpg height=25 width=33%><b><center>Date</center></td></tr>
<?php
$slimit = $action*15;
$llimit = ($action+1)*15;
$pms = mysql_query("SELECT * FROM pm WHERE pm_to='$get_user[id]' ORDER BY id DESC LIMIT $slimit, $llimit");
while ($message = mysql_fetch_array($pms)) {
$from = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE id='$message[pm_from]'"));
?>
<tr><td bgcolor=#111111 height=25 width=0>
<input type=radio name=action value=<?=$message[id] ?> /></td><td bgcolor=#111111 width=33%>- <a href='?page=MessageCenter&view=ViewMessage&action=<?=$message[id]?>' title='<?=$message[text] ?>'>$message[subject]</a></td><td width=33% bgcolor=#111111><a href=?page=Profile&view=$from[id]> $from[username]</a> [$from[id]]</td><td bgcolor=#111111 width=33%> $message[date]</td></tr>";}
</table><table width=70%><tr><td><div align=left><img src=images/arrows_up.png> <input type=submit name=page value=Delete class=form size=5> <input type=submit name=page value=Reply class=form size=5></td></tr></table></form>";
<?php
//the code i need help with
$pmrows = mysql_num_rows(mysql_query("SELECT * FROM pm WHERE pm_to='$get_user[id]'"));
while($pmrows>$llimit){
$i++;
$p=$l-1;
?>
<a href=?page=MessageCenter&action=<?=$p ?>><?=$i ?></a>

<?php } ?>

 

Notice how the HTML is html, and the PHP is PHP. You can mix php with HTML very well.

 

If you follow these 2 suggestions you will get a lot more help around here. Folks don't want to try to decipher messy code thats mixed with a bunch of \" and such.

 

Also, you should make it habit to make sure your values in html are always surrounded by " " like here

 

<input type=hidden name=view value=Message>

could give problems, I have experienced these first hand because of this. It should be

<input type="hidden" name="view" value="Message">

 

I know I did not actually help you with your issue, but that code is VERY difficult to read and decipher. Come back with the code cleaned up and your almost sure to get the problem solved pretty quick. Messy code like that is an almost surefire way to be ignored.

 

I am not trying to be mean or anything, just trying to help you get better results on here

 

Nate

Link to comment
https://forums.phpfreaks.com/topic/50790-using-loops/#findComment-250383
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.