ted_chou12 Posted January 12, 2007 Share Posted January 12, 2007 If I am looping something, for example, a number. and every time this variable goes through a loop, the number increases by one integer, where my php form has a form action that also uses a loop to handle the action, what should I put for the while loop that handles the action?This is what I started with:while ($id != ""){}but $id should be a variable that represents that figure.TedThanks Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/ Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 Probably better with [code=php:0]while ($id < $int){[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-158968 Share on other sites More sharing options...
scottybwoy Posted January 12, 2007 Share Posted January 12, 2007 Well you would need a breaking clause to stop the loop otherwise it can loop continuously and crash your system. So something like[code]<?phpwhile ($id != "" && $id < 25) // If you want it to loop 25 times{ $id++}?>[/code]But thats pretty useless on it's own, not sure what your trying to do. Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-158969 Share on other sites More sharing options...
ted_chou12 Posted January 12, 2007 Author Share Posted January 12, 2007 thanks, i will try them.Ted Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-158970 Share on other sites More sharing options...
ted_chou12 Posted January 12, 2007 Author Share Posted January 12, 2007 um... i think is better to put some code up, because $id is still an undefined variable:this is the loop that generates the request for the action:[code]$id = 0;while ($r = mysql_fetch_array($res)){echo "<input type=checkbox name=$id value=\"$string\">";//ignore string$id ++;}[/code]and this is what takes the action:[code]while ($id != ""){$id = $_POST['id']; //and action will be below$id++;}[/code]but $id is not defined... so i am having a problem...btw, I removed the form tags as well as the $_POST submit to make it simple.ThanksTed Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-158974 Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 Ted, you're not making any sense to me I'm afraid... What do you mean by [i]'and this is what takes the action'[/i]?The first loop looks as though it should work ok. Perhaps you should be telling us what you're trying to achieve, rather than what you think the code should look like?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-158980 Share on other sites More sharing options...
ted_chou12 Posted January 12, 2007 Author Share Posted January 12, 2007 ok. :-\, i think im making it more complicated...:[code]<?phpif(isset($_POST['delete'])){while ($id != ""){$id = $_POST['id'];require("../mysqlconnection.php");$delete = mysql_query("DELETE FROM pm WHERE id='$id' AND username='$username'");$id++;}}?><form name="pm" action="" method="post"><?php$id = 0;while ($r = mysql_fetch_array($res)){echo "<td align=center><input type=checkbox name=$id value=\"". $r['id'] ."\"></td>\n";$id ++;}?><input class="button" name="delete" type="submit" value="Delete Entry" /></form>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-158982 Share on other sites More sharing options...
ted_chou12 Posted January 12, 2007 Author Share Posted January 12, 2007 do you understand it a bit more now? ???or is it still unclear?Ted Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-158983 Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 OK, this is a mess, but we can help you straighten it out.I'm still unclear, as you still haven't said what you're trying to achieve, can you give us an idea? Maybe some examples of what's going to be submitted and what's being pulled by the database. Spend a decent amount of time outlining and explaining the problem and we'll get you a solution sooner and with less posts :)RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-158990 Share on other sites More sharing options...
ted_chou12 Posted January 12, 2007 Author Share Posted January 12, 2007 ok,cool 8)I wish the form to be able to take multiple actions, so the php script will delete each row that is specified in the checkbox, value=". $r['id'] ." is the value of the checkbox, where id is the primary key to mysql table. Since the script loops until all the rows are displayed, therefore, there will be an unfix number of checkboxes, so I want the checkboxes to be able to multiple selected, and therefore deleting multiple rows from the table at once.I think that explains my goal.Ted Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-158993 Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 OK,If I understand it correctly, what you want to do is present multiple rows on a page, e.g. member list, each member has a check box next to their name and I can select the boxes and press the delete button at the bottom of the page to remove them from the database?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-158996 Share on other sites More sharing options...
ted_chou12 Posted January 12, 2007 Author Share Posted January 12, 2007 i think you got my point, but rather than a member list, is a messaage system, each row represents a message. Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-158999 Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 OK, in that case I'd use a different method completely... I'd construct a sql string first of all and remove them that way...[code]<?phpif (isset($_POST['delete'])){ if (!empty($_POST['id'])){ // turn the id array into a comma separated string $id_list = implode(", ", $_POST['id']); // Query to run $sql = "DELETE FROM messages WHERE message_id IN ($id_list)"; mysql_query($sql); }}// Select message list from database$sql = "SELECT message_id, message_text FROM messages WHERE user_id = '{$_SESSION['user_id']}'";$result = mysql_query($sql);// Echo the messages and checkboxeswhile ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo "<input type=\"checkbox\" name=\"id[]\" value=\"{$row['message_id']}\"> {$row['message_text']}<br>\n";}// Echo the submit buttonecho "<input type=\"submit\" name=\"delete\" value=\"Delete\">\n";?>[/code]You could use a foreach() loop or a while() loop, but this way is more efficient as it only queries the database with the delete code once.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-159005 Share on other sites More sharing options...
ted_chou12 Posted January 12, 2007 Author Share Posted January 12, 2007 thanks a lot, but some things inside looks brand new to me, like:{$row['message_id']} {$row['message_text']}and as well as the delete codes, I have a pagination code as well, I am getting a bit confused...here is how to page code looks like originally:[code] <?php //Javascript for the delete, this helps to select all checkboxes?> <SCRIPT LANGUAGE="JavaScript"> <!-- Modified By: Steve Robison, Jr. (stevejr@ce.net) --> <!-- Begin var checkflag = "false"; function check(field) { if (checkflag == "false") { for (i = 0; i < field.length; i++) { field[i].checked = true;} checkflag = "true"; return "Uncheck All"; } else { for (i = 0; i < field.length; i++) { field[i].checked = false; } checkflag = "false"; return "Check All"; } } // End --> </script> <?php //the delete code. if(isset($_POST['delete'])){ while ($id != ""){ $id = $_POST['id']; require("../mysqlconnection.php"); $delete = mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'"); if($delete === false) {echo"<p><b>Sorry, the database is temporary down, please come back later!</b></p>";} else {header("location: confirm.php?confirm=pm_delete");}}}?> <form name="deletepm" action="" method="post"> <?php //below is all the pagination code. //order sort $order = $_GET['order']; if ($order == "senderasc") {$orderby = "ORDER BY username"; $menu1 = "v";} elseif ($order == "senderdes") {$orderby = "ORDER BY username DESC"; $menu1 = "^";} elseif ($order == "subjectasc") {$orderby = "ORDER BY entrytitle"; $menu2 = "v";} elseif ($order == "subjectdes") {$orderby = "ORDER BY entrytitle DESC"; $menu2 = "^";} elseif ($order == "dateasc") {$orderby = "ORDER BY entrydate"; $menu3 = "v";} else {$order = "datedes";$orderby = "ORDER BY entrydate DESC"; $menu3 = "^";} if ($order == "senderasc") {$ordersendermenu = "senderdes";} else {$ordersendermenu = "senderasc";} if ($order == "subjectasc") {$ordersubjectmenu = "subjectdes";} else {$ordersubjectmenu = "subjectasc";} if ($order == "dateasc") {$orderdatemenu = "datedes";} else {$orderdatemenu = "dateasc";} // connect to mysql below require("../mysqlconnection.php"); // Set how many rows to display on each page $limit = 20; // Query the database to get the total number of rows $query_count = "SELECT * FROM privatemessages WHERE username='$username' AND type='inbox'"; $result_count = mysql_query($query_count) or die (mysql_error()); $totalrows = mysql_num_rows($result_count); if(isset($_GET['page'])){ // Checks if the $page variable is empty (not set) $page = $_GET['page']; // If it is empty, we're on page 1 } else { $page = 1; } // Set the start value $startvalue = $page * $limit - ($limit); // Query the database and set the start row and limit $sql = "SELECT * FROM privatemessages WHERE username='$username' AND type='inbox' $orderby LIMIT $startvalue, $limit"; $res = mysql_query($sql) or die (mysql_error()); echo "<table border=0 align=center cellpadding=\"0\" cellspacing=\"1\" width=\"100%\"> <tr height=\"28em\"> <td width=\"28em\" align=center background=\"gradient.gif\"><img src=\"images/xx.gif\"></td> <td width=\"28em\" align=center background=\"gradient.gif\"> <input type=checkbox value=\"Check All\" onClick=\"this.value=check(action)\"></td> <td width=\"80em\" background=\"gradient.gif\"> <a href=\"".$_SERVER['PHP_SELF']."?page=".$page."&order=".$ordersendermenu."\"><b>".$menu1." Sender</b></a></td> <td width=\"160em\" background=\"gradient.gif\"> <a href=\"".$_SERVER['PHP_SELF']."?page=".$page."&order=".$ordersubjectmenu."\"><b>".$menu2." Subject</b></a></td> <td width=\"70em\" background=\"gradient.gif\"> <a href=\"".$_SERVER['PHP_SELF']."?page=".$page."&order=".$orderdatemenu."\"><b>".$menu3." Date</b></a></td> </tr>"; // Do a quick check to see if there are any records to display if(mysql_num_rows($res) == 0){ echo "<tr> <td colspan=3 align=center>No messages found!!</td> </tr>"; } // Start loop through records $id = 0; while ($r = mysql_fetch_array($res)){ $date = strtotime($r['entrydate']) + $row['timeoffset']; $date = date('Y-m-d',$date); $subject = substr($r['entrytitle'], 0, 30); if (strlen($r['entrytitle']) > 30){$subject = "$subject..";} $user = substr($r['user'], 0, 10); if (strlen($r['user']) > 10){$user = "$user..";} if ($r['flag'] == "unread") {$color = "#cccccc";} else {$color = "#f2f2f2";} // This is actually where the form goes... echo "<tr height=\"25em\" bgcolor=".$color.">\n"; echo "<td align=center><img src=\"images/". $r['entryicon'] .".gif\"></td>\n"; echo "<td align=center><input type=checkbox name=$id value=\"". $r['id'] ."\"></td>\n"; echo "<td>". $user ."</td>\n"; echo "<td><a href=\"viewpm.php?id=". $r['id'] ."\">". $subject ."</a></td>\n"; echo "<td>". $date ."</td>\n"; echo "</tr>\n"; $id ++;} // Close the table echo "</table>"; // Start links for pages echo "<p align=center>"; // Sets link for previous 25 and return to page 1 if($page != 1){ $pageprev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=1&order=$order\"><<</a> "; echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageprev&order=$order\">PREV </a> "; }else{ echo "PREV "; } // Find out the total number of pages depending on the limit set $numofpages = $totalrows / $limit; $totalpages = ceil($numofpages); // Loop thru all the pages and echo out the links for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo "[".$i."] "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&order=$order\">$i</a> "; } } // Check for straglers after the limit blocks if(($totalrows % $limit) != 0){ if($i == $page){ echo "[".$i."] "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&order=$order\">$i</a> "; } } // Print out the Next 25 and Goto Last page links if(($totalrows - ($limit * $page)) > 0){ $pagenext = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pagenext&order=$order\">NEXT </a> "; echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$totalpages&order=$order\">>></a> "; }else{ echo("NEXT"); } echo "</p>"; // Free results mysql_free_result($res); // Close mysql connection mysql_close($mysql_conn);?> <input class="button" name="delete" type="submit" value="Delete Entry" onClick="javascript: return checkDelete()" /> </form>[/code]Can you roughly point out where I should put these separatly?ThanksTed Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-159010 Share on other sites More sharing options...
redbullmarky Posted January 12, 2007 Share Posted January 12, 2007 ted - quick question - did you write this code yourself or is it a *cough* Third party script you're asking us to modify for you? Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-159011 Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 [quote author=ted_chou12 link=topic=122076.msg503020#msg503020 date=1168604014]thanks a lot, but some things inside looks brand new to me, like:{$row['message_id']} {$row['message_text']}[/quote]OK, it's just the way that I'm displaying the variables without exiting the echo... You're probably used to writing it like this...[code=php:0]echo '<input type="checkbox" name="id[]" value="' .$row['message_id']. '">' .$row['message_text']. '<br>';[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-159012 Share on other sites More sharing options...
ted_chou12 Posted January 12, 2007 Author Share Posted January 12, 2007 Thanksbtw. script is from craygo... kind of third party...Ted Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-159013 Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 As for where to put it, if you break down what I've provided, it's clearly just two blocks... One that selects and displays the rows and one that deletes them. Try putting them in where you think and see if it works, if not then post back.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-159015 Share on other sites More sharing options...
redbullmarky Posted January 12, 2007 Share Posted January 12, 2007 then please - instead of just getting others to write the code and others to patch them up, you do yourself many many many favours if you actually took the time to understand the code like the rest of us.not being rude, ted - just that almost 99% of every question you have is regarding someone elses script. Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-159017 Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 [quote author=redbullmarky link=topic=122076.msg503027#msg503027 date=1168604606]then please - instead of just getting others to write the code and others to patch them up, you do yourself many many many favours if you actually took the time to understand the code like the rest of us.not being rude, ted - just that almost 99% of every question you have is regarding someone elses script.[/quote]That's a little bit harsh... He's trying look:[quote author=ted_chou12 link=topic=122076.msg503020#msg503020 date=1168604014]thanks a lot, but some things inside looks brand new to me, like:{$row['message_id']} {$row['message_text']}[/quote]I don't suffer fools gladly, but I try my best to steer them on the right path at least twice before reprimanding them :)RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-159020 Share on other sites More sharing options...
ted_chou12 Posted January 12, 2007 Author Share Posted January 12, 2007 thanks, I finished editing already.I should have explained it at the start to make myself clear. :DTed Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-159025 Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 No problem, like I said, if you take your time explaining things in the first instance then you'll get things resolved quicker and in this case maybe even offered some alternative code that will work more efficiently.Had I not have asked and just tried to understand what you were doing with your code, we'd have ended up with something similar to what you'd written that worked, but wasn't very effective.RegardsHuggie. Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-159030 Share on other sites More sharing options...
ted_chou12 Posted January 12, 2007 Author Share Posted January 12, 2007 okay :D Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-159032 Share on other sites More sharing options...
Barand Posted January 12, 2007 Share Posted January 12, 2007 [quote author=ted_chou12 link=topic=122076.msg503020#msg503020 date=1168604014]thanks a lot, but some things inside looks brand new to me, like:{$row['message_id']} {$row['message_text']}[/quote]If those things are new to you then it's very unlikely that the code is your own - it's full of similar associative references. PHPFreaks is not here to provide a free service to modify other peoples code to suit your needs, it's to help you with your own code - read the forum guidelines. Quote Link to comment https://forums.phpfreaks.com/topic/33874-about-looping/#findComment-159038 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.