PHP - Closed Loop
Hello dear friends,
let say we have (function) and that function has one variable which is (id) the (id) could be number any number 1 or 4 or even 49999 whatever. how to make closed loop which i mean run this function in unlimited way each time with new id <--- i can do it by set random() but the problem i've is how to make it repeat itself excuted excuted excuted | | | ect let say for example , i will insert random number in at database table Code: [Select] $id = rand(); mysql_query("INSERT INTO comments(id) VALUES ('$id')"); that is it, how then i can repeat it many many times without 1) refresh 2) rewrite it many times thanks in advance Similar TutorialsOK, I'm nearing the end of a very long chat project and I have one last hurdle to jump, I've saved this task to the near end because I thought it would be a problem as I have no idea what to do or where to start. The problem is exactly what is said in the subject title. I have looked into JavaScript options such as "onbeforeunload" but that is just BAD. The login is controlled by sessions, so when the user closes the browser, the session is ended, but data in the database says different, cookies are not an option I'm afraid. I've had an idea that if I create a special account (or a few) and always leave these accounts logged in, they can check if a user has been inactive for a certain period of time, and then run queries to change things, I know this sound odd but maybe I will not have to create these accounts but general accounts that people make will be able to do this for me, without them even knowing. Please help, any ideas, suggestions, logic or methods that you think will help will be great. Thanks. I have a class I use to handle my various DB objects. I want to close any connections, if needed, in my destructor. I want to do this even though I already know the connection has been closed previously. What I am looking for is a way to test the $connection variable I have to see if it's in need of closing. I can't figure out how to do this. if (isset($this->connection)) { echo "is set"; } if ($this->connection) { echo "here"; } The above would output "is set" and "here", because in both tests say the already closed connection is still there. Can anyone offer any help? Hey All, I have a sessions table that is used to see if a user is online. It only gets deleted when they click the Logout button. What I need to do is have it so it gets deleted even if they close the browser. Should I add a column in the sessions table with a time stamp and check on each page for every user to make sure its great than 30 min? Is there a better way to do this, without so many queries? Thanks i have a script that wrote which updates fields in a mysql database with millions of rows. it just uses a while loop for each row. now the problem is that i forgot to add something to the script, but i already visited the .php file in my browser. i closed the browser and when i refresh the database it still seems to be running even though i closed the browser the script was running on.... is this because the script is still cached in the memory or what? how can i stop it? Hi Everyone, I wonder if somebody could help me. I have been at this for hours and am really struggling. I have a timer which I have set up for a quiz. The quiz opens in a javascript shadow box. When the quiz is opened a session variable for the timer is set up to keep track of the time. What I need is for this session variable to end when the shadow box is closed. I have a onClose hook to peform actions when the shadow box is closed which is in javascript. Obviously I cannot end the session variable in the javascript function due to js being client side and php being server side. Has anyone any suggestions how I can resolve this issue. I need to end this session variable as if the user exits the quiz and then selects another quiz to do the timer uses the current time session rather than assigning the new time value for that particular quiz. Thanks in advance. Edd Hello, How do I get a user's session destroyed when he closes his browser? Thanks in advance Hello. I want to display all photos from the database. The problem is that it displays nothing. I added a foreach loop in html but further it not display. I'am beginner, I hope someone help me where I make mistake. I care about separation logic from view.
class photo_display { private $database; private $userData; public function __construct($database) { $this->database = $database; } public function display() { $query = $this->database->connect()->prepare("SELECT photo FROM photo"); $query->execute(); if ($query->rowcount()) { $row = $query->fetchAll(); } $this->userData = $row; } public function display_Photos(string $colName) { return (isset($this->userData[$colName])) ? $this->userData[$colName] : []; } } <div class='container-fluid bg-dark' id='profil'> <div class='row'> <div class=" col-12 col-sm-12 col-md-4 col-lg-4 col-xl-4 order-5 order-12 order-6 order-md-4"> <div class="row" id="margin"> <div class="col-xl-12 d-flex justify-content-center" id="test"> <?php foreach ($photo_display->display_Photos('photo') as $value) { ?> <img src ="<?php echo var_dump($value) ?>" />; <?php } ?> </div> </div> </div>
I made a bug which caused endless loop and endless printing. When I noticed that, I closed the browsers tab, fixed the bug, open a new tab and re-run. I was assuming the server would start from the beginning, but instead I noticed that some variables were active and the program continued from where it stopped. How can I make the program to stop when the client tab is terminated? Ok this is some noob question here I have a page that has few separate pieces of php code embeded within html code. Each piece is separated with Code: [Select] <?php ... ?> tags, everything works fine. I need to assign a variable in first (from the top) piece of php code. Then I want to be able to use that variable in all others php pieces across this same page.... I searched and searched and first tried to do it like : First piece: Code: [Select] <?php function id() { $id="blah"; } ?> Second piece : Code: [Select] <?php id(); ... echo $id; ?> didn't work, then I tried First place: Code: [Select] <?php $id="blah"; function id() { global $id; } ?> Second piece : Code: [Select] <?php echo $id; ?> After struggling and reading for few days all about global, static, and regular variables i finally decided to ask professionals to help.... please let me know if you need me to make the question description more clear or smn This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=352282.0 I GET QuoteFatal error: TPL: [in line 0]: syntax error: file 'm/pg/_categories' does not exist in C:\WebServ\httpd\libs\tpl\class.template.php on line 943 unset($_templatelite_tpl_vars); else: $_templatelite_tpl_vars = $this->_vars; echo $this->_fetch_compile_include($this->_vars['TPLx'].'m/pg/_categories'.$this->_vars['HTML'], array()); $this->_vars = $_templatelite_tpl_vars; WITH FILES http://testynarkotykowe.j13x.pl/index.txt && http://testynarkotykowe.j13x.pl/indexpg.txt HOW TO FIX IT Hey.
So the issue I'm having is consecutive loops on semi-large arrays, over and over. Consider this array:
$firstArray = array( 'row1' => array( 'dates' => array( '2014-01-01' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-01-02' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-01-03' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-01-04' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-01-05' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-01-06' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-01-07' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), ) ), 'row2' => array( 'dates' => array( '2014-02-01' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-02-02' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-02-03' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-02-04' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-02-05' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-02-06' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-02-07' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-02-08' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), '2014-02-09' => array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7', 'key8' => 'value8', 'key9' => 'value9', 'key10' => 'value10'), ) ) );Originally the data comes from ~2-3 database tables, of course. But to ilustrate the point, this is how the main array looks like. This array usually contains anywhere between 10-50 rows, each row containing at least 10 dates, with 10 key/values each. And after setting up all the data, it needs to be processed. Currently this is how a friend of mine did it.. $placeDataHere = array(); foreach($firstArray as $key => $dates) { foreach($dates as $date => $values) { foreach($values as $key => $value) { $placeDataHere['DV_' . $date]['SM_' . $key] = 'KS_' . $value; //Followed by another ~50-70 lines of processing the 3 loop's data.. ... ... .... .... .... .... .... .... } } }Obviously this isn't good practise, but we can't seem to figure out a better way of doing it, since both the data and the loops are horribly nested. This loop and setup of $firstArray is run anywhere between 10-20 times/request, due to amount of users we wish to process. So, the result is that this code can take up to over 2-3 minutes to complete, which isn't really optimal performance. In short my question is, are there any better methods of handling this with the data setup we currently have? Hello all, I'm an amateur at PHP coding, and am currently enrolled in a PHP and MySQL class that uses the PHP Programming with MySQL textbook, by Don Gosselin. I'm trying to get this simple Shopping Cart script to work, because it's the step-by-step example script for Chapter 11, but I keep getting the following errors on ShowCart.php when I try to add an item to the cart. Quote Warning: mysqli_query() [function.mysqli-query]: Couldn't fetch mysqli in C:\xampplite\htdocs\ShoppingCart.php on line 78 Warning: mysqli_errno() [function.mysqli-errno]: Couldn't fetch mysqli in C:\xampplite\htdocs\ShoppingCart.php on line 80 Warning: mysqli_error() [function.mysqli-error]: Couldn't fetch mysqli in C:\xampplite\htdocs\ShoppingCart.php on line 81 Line 78: $QueryResult = mysqli_query($this->DBConnect, $SQLstring) Line 80 and 81: . "<p>Error code " . mysqli_errno($this->DBConnect) . ": " . mysqli_error($this->DBConnect)) . "</p>"; I've read "Couldn't fetch mysqli" errors are because the connection was closed prior to those lines, but I don't see any indication of a connection closure. I have no idea what to do, as all the code was given to me in the book, I merely copied it down. Here's the ShoppingCart.php code:<?php class ShoppingCart { private $DBConnect = ""; private $DBName = ""; private $TableName = ""; private $Orders = array(); private $OrderTables = array(); function construct() { $this->DBConnect = mysqli_connect("localhost", "root", "passHere"); if (mysqli_connecT_errno()) die("<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysqli_connect_errno() . ": " . mysqli_connect_error()) . "</p>"; } public function setDatabase($Database) { $this->DBName = $Database; $this->DBConnect->select_db($this->DBName) Or die("<p>Unable to select the databbase.</p>" . "<p>Error code " . mysqli_errno($this->DBConnect) . ": " . mysqli_error($this->DBConnect)) . "</p>"; } public function setTable($Table) {echo $table."<br />"; $this->TableName = $Table; } public function getProductList() { $SQLstring = "SELECT * FROM $this->TableName"; $QueryResult = $this->DBConnect->query($SQLstring) Or die("<p>Error code " . mysqli_errno($this->DBConnect) . ": " . mysqli_error($DBConnect)) . "</p>"; echo "<table width='100%' border='1'>"; echo "<tr><th>Product</th><th>Description</th><th>Price Each</th><th>Select Item</th></tr>"; $Row = $QueryResult->fetch_row(); do { echo "<tr><td>{$Row[1]}</td>"; echo "<td>{$Row[2]}</td>"; printf("<td align='center'>$%.2f</td>", $Row[3]); echo "<td align ='center'> <a href='ShowCart.php?PHPSESSID=" . session_id() . "&operation=addItem&productID=" . $Row[0] . "'>Add</a></td></tr>"; $Row = $QueryResult->fetch_row(); } while ($Row); echo "</table>"; } public function addItem() { $ProdID = $_GET['productID']; if (array_key_exists($ProdID, $this->Orders)) exit("<p>You already selected that item! Click your browser's back button to return to the previous page.</p>"); $this->Orders[$ProdID] = 1; $this->OrderTable[$ProdID] = $this->TableName; } function _wakeup() { $this->DBConnect = new mysqli("localhost", "staticlo_shane", "shinfoosh"); if (mysqli_connect_errno()) die("<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysqli_connect_errno() . ": " . mysqli_connect_error()) . "</p>"; $this->DBConnect->Select_db($this->DBName) Or die("<p>Unable to select the database.</p>" . "<p>Error code " . mysqli_errno($$this->DBConnect) . ": " . mysqli_error($this->DBConnect)) . "</p>"; } public function showCart() { if (empty($this->Orders)) echo "<p>Your shopping cart is empty!</p>"; else { echo "<table width='100%' border='1'>"; echo "<tr><th>Remove Item</th><th>Product</th><th>Quantity</th><th> Price Each</th></tr>"; $Total = 0; foreach($this->Orders as $Order) { $SQLstring = "SELECT * FROM " . $this->OrderTable[key($this->Orders)] . " WHERE productID='" . key($this->Orders) . "'"; $QueryResult = mysqli_query($this->DBConnect, $SQLstring) Or die("<p>Unable to perform the query.</p>" . "<p>Error code " . mysqli_errno($this->DBConnect) . ": " . mysqli_error($this->DBConnect)) . "</p>"; $Row = mysqli_fetch_row($QueryResult); echo "<td align='center'>"; echo "<a href='ShowCart.php?PHPSESSID=" . session_id() . "&operation=removeItem&productID=" . $Row[0] . "'>Remove</a></td>"; echo "<td>{$Row[1]}</td>"; echo "<td align='center''>$Order "; echo "<a href='ShowCart.php?PHPSESSID=" . session_id() . "&operation=addOne&productID=" . $Row[0] . "'>Add</a>"; echo "<a href='ShowCart.php?PHPSESSID=" . session_id() . "&operation=removeOne&productID=" . $Row[0] . "'>Remove</a>"; echo "</td>"; printf("<td align='center'>$%.2f</td></tr>", $Row[3]); $Total += $Row[3] * $Order; next($this->Orders); echo "<td align='center' colspan='2'><strong>Your shopping cart contains " . count($this->Orders) . " product(s).</strong></td>"; printf("<td align='center'><strong>Total: $%.2f</stong> </td>", $Total); echo "</table>"; } echo "<tr><td align='center'><a href='ShowCart.php?PHPSESSID=" . session_id() . "&operation=emptyCart'><strong> Empty Cart</strong></a></td>"; } } public function removeItem() { $ProdID = $_GET['productID']; unset($this->Orders[$ProdID]); unset($this->OrderTable[$ProdID]); } function emptyCart() { $this->Orders = array(); $this->OrderTale = array(); } function _destruct() { $this->DBConnect->close(); } public function addOne() { $ProdID = $_GET['productID']; $this->Orders[$ProdID] += 1; } public function removeOne() { $ProdID = $_GET['productID']; $this->Orders[$ProdID] -= 1; if ($this->Orders[$ProdID] == 0) $this->removeItem(); } } ?> Here is the ShowCart.php code:<?php session_start(); require_once("ShoppingCart.php"); if (!isset($_SESSION['curCart'])) header("location:GosselinGourmetGoods.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name="Robots" content="nofollow, noindex" /> <link rel="stylesheet" type="text/css" media="screen" href="php_styles.css" /> </head> <body> <h1>Gosselin Gourmet Goods</h1> <h2>Shop by Category</h2> <p><a href="GosselinGourmetCoffees.php">Gourmet Coffees</a><br /> <a href="GosselinGourmetOlives.php">Specialty Olives</a><br /> <a href="GosselinGourmetSpices.php">Gourmet Spices</a></p> <?php $Cart = unserialize($_SESSION['curCart']); if (isset($_GET['operation'])) { if ($_GET['operation'] == "addItem") $Cart->addItem(); if ($_GET['operation'] == "removeItem") $Cart->removeItem(); if ($_GET['operation'] == "emptyCart") $Cart->emptyCart(); if ($_GET['operation'] == "addOne") $Cart->addOne(); if ($_GET['operation'] == "removeOne") $Cart->removeOne(); } $Cart->showCart(); $_SESSION['curCart'] = serialize($Cart); ?> </body> </html> Here is the product page for "Specialty Olives" which is identical to the Coffees and Spices pages, save for the Table name changed to their respective products: <?php session_start(); require_once("ShoppingCart.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Gosselin Gourmet Goods</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name="Robots" content="nofollow, noindex" /> <link rel="stylesheet" type="text/css" media="screen" href="php_styles.css" /> </head> <body> <h1>Gosselin Gourmet Goods</h1> <h2>Shop by Category</h2> <p><a href="GosselinGourmetCoffees.php">Gourmet Coffees</a><br /> <a href="GosselinGourmetOlives.php">Specialty Olives</a><br /> <a href="GosselinGourmetSpices.php">Gourmet Spices</a></p> <h2>Speciality Olives</h2> <?php $Database = "gosselin_gourmet"; $Table = "olives"; $Cart=!empty($_SESSION['curCart'])?unserialize($_SESSION['curCart']):new ShoppingCart(); $Cart->construct(); $Cart->setDatabase($Database); $Cart->setTable($Table); $Cart->getProductList(); $_SESSION['curCart'] = serialize($Cart); ?> <p><a href='<?php echo "ShowCart.php?PHPSESSID=" . session_id() ?>'>Show Shopping Cart</a></p> </body> </html> Below is my output on the browser: Student: Kevin Smith (u0867587) Course: INFO101 - Bsc Information Communication Technology Course Mark 70 Grade Year: 3 Module: CHI2550 - Modern Database Applications Module Mark: 41 Mark Percentage: 68 Grade: B Session: AAB Session Mark: 72 Session Weight Contribution 20% Session: AAE Session Mark: 67 Session Weight Contribution 40% Module: CHI2513 - Systems Strategy Module Mark: 31 Mark Percentage: 62 Grade: B Session: AAD Session Mark: 61 Session Weight Contribution 50% Now where it says course mark above it says 70. This is incorrect as it should be 65 (The average between the module marks percentage should be 65 in the example above) but for some stange reason I can get the answer 65. I have a variable called $courseMark and that does the calculation. Now if the $courseMark is echo outside the where loop, then it will equal 65 but if it is put in while loop where I want the variable to be displayed, then it adds up to 70. Why does it do this. Below is the code: Code: [Select] $sessionMark = 0; $sessionWeight = 0; $courseMark = 0; $output = ""; $studentId = false; $courseId = false; $moduleId = false; while ($row = mysql_fetch_array($result)) { $sessionMark += round($row['Mark'] / 100 * $row['SessionWeight']); $sessionWeight += ($row['SessionWeight']); $courseMark = ($sessionMark / $sessionWeight * 100); if($studentId != $row['StudentUsername']) { //Student has changed $studentId = $row['StudentUsername']; $output .= "<p><strong>Student:</strong> {$row['StudentForename']} {$row['StudentSurname']} ({$row['StudentUsername']})\n"; } if($courseId != $row['CourseId']) { //Course has changed $courseId = $row['CourseId']; $output .= "<br><strong>Course:</strong> {$row['CourseId']} - {$row['CourseName']} <strong>Course Mark</strong>" round($courseMark) "<strong>Grade</strong> <br><strong>Year:</strong> {$row['Year']}</p>\n"; } if($moduleId != $row['ModuleId']) { //Module has changed if(isset($sessionsAry)) //Don't run function for first record { //Get output for last module and sessions $output .= outputModule($moduleId, $moduleName, $sessionsAry); } //Reset sessions data array and Set values for new module $sessionsAry = array(); $moduleId = $row['ModuleId']; $moduleName = $row['ModuleName']; } //Add session data to array for current module $sessionsAry[] = array('SessionId'=>$row['SessionId'], 'Mark'=>$row['Mark'], 'SessionWeight'=>$row['SessionWeight']); } //Get output for last module $output .= outputModule($moduleId, $moduleName, $sessionsAry); //Display the output echo $output; I think the problem is that it is outputting the answer of the calculation only for the first session mark. How in the while loop can I do it so it doesn't display it for the first mark only but for all the session marks so that it ends up showing the correct answer 65 and not 72? Hey guys, Got another question im hoping someone can help me with. I have a foreach loop (for use in a mysql query): foreach ($interests as $interest) { $query .= "($id, $interest), "; } problem is i do not want the comma(,) in the last loop. Is there some kinda of function i can use so it does not insert it on last loop? Or should i just use a for loop with a nested if loop? something like ; for($i=0; $i < count($interests); $i++){ $query .= "($id, '$interests[$i]')"; if($i + 1 < count($interests)) { $query .= ", "; } } Cheers guys I am working to echo the results in a while or for loop... Both of my sample codes work, but the results are wrong! The while loop ONLY echos a result IF the first record in the postings table matches the id passed (does not display a result unless the first record has a match) The if loop displays ALL listings with the same name (counts them all) so there are no unique listings! <?php $posts_by_city_sql = "SELECT * FROM postings WHERE id='$_GET[id]'"; $posts_by_city_results = (mysqli_query($cxn, $posts_by_city_sql)) or die("Was not able to grab the Postings!"); /* While Loop */ while($posts_by_city_row = mysqli_fetch_array($posts_by_city_results)) { echo "<li><a href='posting_details.php?id=$posts_by_city_row[id]'>$posts_by_city_row[title]</a></li>"; } /* For Loop */ $posts_by_city_row = mysqli_fetch_array($posts_by_city_results); for ($i=0; $i<sizeof($posts_by_city_row); $i++) { echo "<li><a href='posting_details.php?id=$posts_by_city_row[id]'>$posts_by_city_row[title]</a></li>"; } ?> Results with for loop (there are 7 total unique book names, but it's just counting the first match on id 7 times like below): AJAX for Beginners AJAX for Beginners AJAX for Beginners AJAX for Beginners AJAX for Beginners AJAX for Beginners AJAX for Beginners AJAX for Beginners Good Evening - I am in the process of trying to call back a list of categories and sub categories using a WHILE LOOP inside of a WHILE LOOP. It works on a different part of the site within the admin panel but not here. Here it only calls one sub category and moves on to the next parent category instead of finishing the loop and pulling all sub categories out... // CATEGORIES $query = "SELECT * FROM cat"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $catid = $row['id']; $catname = $row['name']; $output .= "<li class=\"level0 nav-2 parent\" onmouseover=\"toggleMenu(this,1)\" onmouseout=\"toggleMenu(this,0)\"> <a href=\"product.php?cat=$catid\"> <span>$catname</span> </a>\n"; $querynav = "SELECT * FROM subcat WHERE pid = '$catid'"; $resultnav = mysql_query($querynav); while($array = mysql_fetch_array($resultnav, MYSQL_ASSOC)) { $subcatid = $row['id']; $subcatname = $row['name']; $output .= "<ul class=\"level0\"> <li class=\"level1 nav-2-1 first\"> <a href=\"product.php?cat=$catid&subid=$subcatid\"> <span>$subcatname</span> </a> </li> </ul> </li>"; } } I'm having a really bad brain fart and I cannot get the logic correct here lol. I need divs wrapped around every set of 5 images so if there is a total of 8 images then there would be 2 sets of divs (first with 5 images, second with 3 images) and if there was 12 images then there would be 3 sets of divs (first two with five images, last one with 2 images) etc etc What am I doing wrong and is there an easier way to do this? http://pastebin.com/QfsFxe4G Thanks! Hi, I am new to PHP. Can someone explain me clearly how to use while loop inside while loop to print "*" in square shape. Thank you for your help. Hi.. I have while loop inside while loop but I encountered problem in displaying data inside while loop or the second while loop. here is my code: Code: [Select] <?php error_reporting(0); $con = mysql_connect('localhost', 'root',''); if (!$con) { echo 'failed'; die(); } mysql_select_db("mes", $con); ?> <script type="text/javascript"> function showDetails(pcode) { var clickElement = pcode.value; var click_id = pcode.id; // var value_ = document.getElementById(click_id).checked // = document.getElementById(clickElement).checked; //var Table = (document.getElementsByName('list')[0].value); var Table = document.getElementById('kanban_list'); var rows = Table.rows; var strSelect = document.getElementById(click_id).value; //alert(strSelect) for (var i = 0; i < rows.length; i++) { var row = rows[i]; //row.style.display = (row.id.substr(0,3) == strSelect) ? 'none' : ''; //row.style.display = (row.id.substr(0,3) == strSelect) ? // row.style.display = 'none'; // row.style.display = ''; if (row.id.substr(0,3) == strSelect) { row.style.display = ((document.getElementById(click_id).checked) == false) ? 'none' : '' } //(document.getElementById(click_id).checked == false) ? 'none' : '' : ''; } } </script> <?php $sql = "SELECT kc.PCODE, kc.count_wip_chemical_weighing, kc.count_wip_compounding, kc.count_wip_extrusion, kc.count_wip_forming, kc.count_wip_deflashing, kc.kanban, kc.virtual, p.max_lot, p.min_lot FROM kanban_checker kc JOIN plan p ON kc.PCODE = p.PCODE ORDER BY p.PCODE"; $result = mysql_query($sql, $con); ?><label>Display Details:</label><input onclick='showDetails(this);' id='chkDetail' type='checkbox' checked='checked' value='wip'/> <?php echo "<table id='kanban_list'>"; echo "<tr> <th> PCODE </th> <th> LOT CODE </th> <th> CHEMICAL WEIGHING </th> <th> COMPOUNDING </th> <th> EXTRUSION </th> <th> FORMING </th> <th> DEFLASHING </th> <th> KANBAN </th> <th> VIRTUAL </th> <th> MAX LOT </th> <th> MIN LOT </th> </tr>"; while($row = mysql_fetch_assoc($result)){ echo "<tr> <td>$row[PCODE]</td> <td> </td> <!-- <td>$row[LOT_CODE]</td> --> <td>$row[count_wip_chemical_weighing]</td> <td>$row[count_wip_compounding]</td> <td>$row[count_wip_extrusion]</td> <td>$row[count_wip_forming]</td> <td>$row[count_wip_deflashing]</td> <td>$row[kanban]</td> <td>$row[virtual]</td> <td>$row[max_lot]</td> <td>$row[min_lot]</td> </tr>"; $sql = "SELECT kd.LOT_CODE, kd.wip_chemicalweighing, kd.wip_compounding, kd.wip_extrusion, kd.wip_forming, kd.wip_deflashing FROM kanban_data kd JOIN plan p ON kd.PCODE = p.PCODE ORDER BY p.PCODE "; $result_kanban_data = mysql_query($sql, $con); while($row_data = mysql_fetch_assoc($result_kanban_data)){ echo "<tr id='wip'> <td></td> <td> $row_data[LOT_CODE]</td> <td> $row_data[wip_chemicalweighing]</td> <td> $row_data[wip_compounding]</td> <td> $row_data[wip_extrusion]</td> <td> $row_data[wip_forming]</td> <td> $row_data[wip_deflashing]</td> </tr>"; } } echo "</table>"; ?> I attach the image of result from this code But the output that I want is all LOT_CODE with P35 PCODE will only display below P35 and all LOTCODE with P35M PCODE will only display below P35M. Thank you I hope somebody can help me.. |