btd1924 Posted March 27, 2008 Share Posted March 27, 2008 I have got a nested repeat region working until the last record in the parent repeat. It stops the child repeat from showing the rows. Could someone have a look and see what I have missed? Code: <?php $query = "SELECT * FROM sections ORDER BY sectionOrder ASC"; $result = mysql_query($query); $total_records = mysql_num_rows($result); // the number of records in your result set $num_cols = 1; // the number of columns $num_rows = ceil($total_records / $num_cols); // the number of rows $num = 0; // don't change this value, this is the first number of each record inside a record set echo "<table>\n"; // next the loop for the table rows for ($rows = 0; $rows < $num_rows; $rows++) { echo "<tr>\n"; // this is the loop for the table columns for ($cols = 0; $cols < $num_cols; $cols++) { if ($num < $total_records) { // show records if available (reduce by one because the first record is no. "0" (zero) // first create variables with the values of the current record $titel = mysql_result($result, $num, "sectionName"); // you have to chenge the names here to fit your own sql statement $catID = mysql_result($result, $num, "sectionID"); echo "<td><b>".$titel."</b><td>\n"; } else { // show an empty cell echo "<td> </td>\n"; } $num++; // raise the number by one for the next record } echo "</tr>\n"; // there are no more cols in this row, close the table row tag // Grab products from table Where pSection = sectionID $pquery = "SELECT * FROM products WHERE pSection='$catID'"; $presult = mysql_query($pquery); $ptotal_records = mysql_num_rows($presult); // the number of records in your result set $pnum_cols = 2; // the number of columns $pnum_rows = ceil($ptotal_records / $pnum_cols); // the number of rows $pnum = 0; // don't change this value, this is the first number of each record inside a record set for ($prows = 0; $prows < $pnum_rows; $prows++) { echo "<tr>\n"; // this is the loop for the table columns for ($pcols = 0; $pcols < $pnum_cols; $pcols++) { if ($num < $total_records) { // show records if available (reduce by one because the first record is no. "0" (zero) // first create variables with the values of the current record $ptitel = mysql_result($presult, $pnum, "pName"); // you have to chenge the names here to fit your own sql statement $pdescription = mysql_result($presult, $pnum, "descr"); $purl = mysql_result($presult, $pnum, "link"); echo "<td><b>".$ptitel."</b><br>".$pdescription."<i><a href=\"".$purl."\">Visit here!</a></i><td>\n"; } else { // show an empty cell echo "<td> </td>\n"; } $pnum++; // raise the number by one for the next record } echo "</tr>\n"; // there are no more cols in this row, close the table row tag } } echo "</table>\n"; // end of the region = closing tag for the table element ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/ Share on other sites More sharing options...
wildteen88 Posted March 27, 2008 Share Posted March 27, 2008 It'll be better if you looked into converting your for loops and mysql_result functions calls into a nice simple while loop: while($row = mysql_fetch_assoc($sql_query_result)) { echo '<pre>' . print_r($row, true) .' </pre>'; } That will require far less coding. You should also look into using SQL Joins. Rather than calling multiple queries you'd just have one query and a single while loop, which will make for some nice clean code. Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/#findComment-502287 Share on other sites More sharing options...
btd1924 Posted March 27, 2008 Author Share Posted March 27, 2008 I love the idea of less code, could you elaborate on how this can be done. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/#findComment-502321 Share on other sites More sharing options...
wildteen88 Posted March 27, 2008 Share Posted March 27, 2008 The very basic code, What does this output: $query = "SELECT * FROM sections s, products p WHERE p.pSection=s.sectionID ORDER BY sectionOrder ASC"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { echo '<pre>' . print_r($row, true) .' </pre>'; } The major step here is using an SQL join to query two tables at once. Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/#findComment-502334 Share on other sites More sharing options...
btd1924 Posted March 27, 2008 Author Share Posted March 27, 2008 This is great. Way over my head, But I would like to learn how to make this work for what I am doing. If you have time can you help me put it together? I want to have categories on a page and then under each category would be a list of products. sample: Books Php for dummies PHP behind the code eTraining PHP CMS PHP Cool Functions Thanks for the help. Also thanks for the quick responses. Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/#findComment-502353 Share on other sites More sharing options...
wildteen88 Posted March 27, 2008 Share Posted March 27, 2008 I can help yes. However I need to know the output of the code I provided earlier. Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/#findComment-502354 Share on other sites More sharing options...
btd1924 Posted March 27, 2008 Author Share Posted March 27, 2008 Here it is: Array ( [sectionID] => 5 [sectionName] => Books [sectionName2] => [sectionName3] => [sectionWorkingName] => Books [sectionurl] => [sectionImage] => [sectionDescription] => All Products [sectionDescription2] => [sectionDescription3] => [topSection] => 0 [rootSection] => 1 [sectionOrder] => 1 [sectionDisabled] => 0 [pID] => vsb100 [pName] => Valueselling Book: Driving Up Sales One Conversation at a Time [pName2] => [pName3] => [pSection] => 5 [pDescription] => [pDescription2] => [pDescription3] => [pLongdescription] => [pLongdescription2] => [pLongdescription3] => [pImage] => prodimages/valueselling_book.jpg [pImage2] => [pImage3] => [pImage4] => [pImage5] => [pLargeImage] => [pLargeImage2] => [pLargeImage3] => [pLargeImage4] => [pLargeImage5] => [pGiantImage] => [pGiantImage2] => [pGiantImage3] => [pGiantImage4] => [pGiantImage5] => [pDownload] => [pPrice] => 16.95 [pListPrice] => 0 [pWholesalePrice] => 0 [pShipping] => 0 [pShipping2] => 0 [pWeight] => 0 [pDisplay] => 1 [pSell] => 1 [pStaticPage] => 0 [pStockByOpts] => 0 [pRecommend] => 1 [pExemptions] => 0 [pInStock] => 0 [pDropship] => 0 [pManufacturer] => 0 [pDims] => [pSKU] => [pDateAdded] => 2008-03-17 [pTax] => [pOrder] => 1 ) Array ( [sectionID] => 5 [sectionName] => Books [sectionName2] => [sectionName3] => [sectionWorkingName] => Books [sectionurl] => [sectionImage] => [sectionDescription] => All Products [sectionDescription2] => [sectionDescription3] => [topSection] => 0 [rootSection] => 1 [sectionOrder] => 1 [sectionDisabled] => 0 [pID] => TDSS100 [pName] => Top Dog Sales Secrets [pName2] => [pName3] => [pSection] => 5 [pDescription] => [pDescription2] => [pDescription3] => [pLongdescription] => [pLongdescription2] => [pLongdescription3] => [pImage] => prodimages/ [pImage2] => [pImage3] => [pImage4] => [pImage5] => [pLargeImage] => prodimages/ [pLargeImage2] => [pLargeImage3] => [pLargeImage4] => [pLargeImage5] => [pGiantImage] => [pGiantImage2] => [pGiantImage3] => [pGiantImage4] => [pGiantImage5] => [pDownload] => [pPrice] => 24.95 [pListPrice] => 0 [pWholesalePrice] => 0 [pShipping] => 0 [pShipping2] => 0 [pWeight] => 0 [pDisplay] => 1 [pSell] => 1 [pStaticPage] => 0 [pStockByOpts] => 0 [pRecommend] => 0 [pExemptions] => 0 [pInStock] => 0 [pDropship] => 0 [pManufacturer] => 0 [pDims] => [pSKU] => [pDateAdded] => 2008-03-26 [pTax] => [pOrder] => 0 ) Array ( [sectionID] => 7 [sectionName] => Reinforcement Tools [sectionName2] => [sectionName3] => [sectionWorkingName] => Reinforcement Tools [sectionurl] => [sectionImage] => [sectionDescription] => [sectionDescription2] => [sectionDescription3] => [topSection] => 0 [rootSection] => 1 [sectionOrder] => 2 [sectionDisabled] => 0 [pID] => vpn001 [pName] => Value Prompter® Worksheet Note Pad [pName2] => [pName3] => [pSection] => 7 [pDescription] => [pDescription2] => [pDescription3] => [pLongdescription] => [pLongdescription2] => [pLongdescription3] => [pImage] => prodimages/ValuePrompter_notepad.jpg [pImage2] => [pImage3] => [pImage4] => [pImage5] => [pLargeImage] => [pLargeImage2] => [pLargeImage3] => [pLargeImage4] => [pLargeImage5] => [pGiantImage] => [pGiantImage2] => [pGiantImage3] => [pGiantImage4] => [pGiantImage5] => [pDownload] => [pPrice] => 5 [pListPrice] => 0 [pWholesalePrice] => 0 [pShipping] => 0 [pShipping2] => 0 [pWeight] => 0 [pDisplay] => 1 [pSell] => 1 [pStaticPage] => 0 [pStockByOpts] => 0 [pRecommend] => 1 [pExemptions] => 0 [pInStock] => 0 [pDropship] => 0 [pManufacturer] => 0 [pDims] => [pSKU] => [pDateAdded] => 2008-03-17 [pTax] => [pOrder] => 5 ) Array ( [sectionID] => 7 [sectionName] => Reinforcement Tools [sectionName2] => [sectionName3] => [sectionWorkingName] => Reinforcement Tools [sectionurl] => [sectionImage] => [sectionDescription] => [sectionDescription2] => [sectionDescription3] => [topSection] => 0 [rootSection] => 1 [sectionOrder] => 2 [sectionDisabled] => 0 [pID] => vp001 [pName] => Value Prompter® Mouse Pad [pName2] => [pName3] => [pSection] => 7 [pDescription] => [pDescription2] => [pDescription3] => [pLongdescription] => [pLongdescription2] => [pLongdescription3] => [pImage] => prodimages/ValuePrompter.jpg [pImage2] => [pImage3] => [pImage4] => [pImage5] => [pLargeImage] => [pLargeImage2] => [pLargeImage3] => [pLargeImage4] => [pLargeImage5] => [pGiantImage] => [pGiantImage2] => [pGiantImage3] => [pGiantImage4] => [pGiantImage5] => [pDownload] => [pPrice] => 7.5 [pListPrice] => 0 [pWholesalePrice] => 0 [pShipping] => 0 [pShipping2] => 0 [pWeight] => 0 [pDisplay] => 1 [pSell] => 1 [pStaticPage] => 0 [pStockByOpts] => 0 [pRecommend] => 1 [pExemptions] => 0 [pInStock] => 0 [pDropship] => 0 [pManufacturer] => 0 [pDims] => [pSKU] => [pDateAdded] => 2008-03-17 [pTax] => [pOrder] => 2 ) Array ( [sectionID] => 8 [sectionName] => E-learning courses [sectionName2] => [sectionName3] => [sectionWorkingName] => E-learning courses [sectionurl] => [sectionImage] => [sectionDescription] => [sectionDescription2] => [sectionDescription3] => [topSection] => 0 [rootSection] => 1 [sectionOrder] => 3 [sectionDisabled] => 0 [pID] => en100 [pName] => ValueSelling Essentials™ Negotiating [pName2] => [pName3] => [pSection] => 8 [pDescription] => [pDescription2] => [pDescription3] => [pLongdescription] => [pLongdescription2] => [pLongdescription3] => [pImage] => prodimages/Negotiating.jpg [pImage2] => [pImage3] => [pImage4] => [pImage5] => [pLargeImage] => [pLargeImage2] => [pLargeImage3] => [pLargeImage4] => [pLargeImage5] => [pGiantImage] => [pGiantImage2] => [pGiantImage3] => [pGiantImage4] => [pGiantImage5] => [pDownload] => [pPrice] => 129 [pListPrice] => 0 [pWholesalePrice] => 0 [pShipping] => 0 [pShipping2] => 0 [pWeight] => 0 [pDisplay] => 1 [pSell] => 1 [pStaticPage] => 0 [pStockByOpts] => 0 [pRecommend] => 1 [pExemptions] => 0 [pInStock] => 0 [pDropship] => 0 [pManufacturer] => 0 [pDims] => [pSKU] => [pDateAdded] => 2008-03-17 [pTax] => [pOrder] => 3 ) Array ( [sectionID] => 8 [sectionName] => E-learning courses [sectionName2] => [sectionName3] => [sectionWorkingName] => E-learning courses [sectionurl] => [sectionImage] => [sectionDescription] => [sectionDescription2] => [sectionDescription3] => [topSection] => 0 [rootSection] => 1 [sectionOrder] => 3 [sectionDisabled] => 0 [pID] => ets001 [pName] => ValueSelling Essentials™ Team Selling [pName2] => [pName3] => [pSection] => 8 [pDescription] => [pDescription2] => [pDescription3] => [pLongdescription] => [pLongdescription2] => [pLongdescription3] => [pImage] => prodimages/team_selling.jpg [pImage2] => [pImage3] => [pImage4] => [pImage5] => [pLargeImage] => [pLargeImage2] => [pLargeImage3] => [pLargeImage4] => [pLargeImage5] => [pGiantImage] => [pGiantImage2] => [pGiantImage3] => [pGiantImage4] => [pGiantImage5] => [pDownload] => [pPrice] => 129 [pListPrice] => 0 [pWholesalePrice] => 0 [pShipping] => 10 [pShipping2] => 0 [pWeight] => 6 [pDisplay] => 1 [pSell] => 1 [pStaticPage] => 0 [pStockByOpts] => 0 [pRecommend] => 1 [pExemptions] => 0 [pInStock] => 10 [pDropship] => 0 [pManufacturer] => 0 [pDims] => [pSKU] => [pDateAdded] => 2008-03-17 [pTax] => [pOrder] => 4 ) Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/#findComment-502359 Share on other sites More sharing options...
wildteen88 Posted March 27, 2008 Share Posted March 27, 2008 Try the following: $query = "SELECT * FROM sections s, products p WHERE p.pSection=s.sectionID ORDER BY sectionOrder ASC"; $result = mysql_query($query); $prev_section = null; $sectionHeading = false; echo '<table border="1" cellpadding="5" cellspacing="2">'; while($row = mysql_fetch_assoc($result)) { if($prev_section != $row['sectionName']) { $prev_section = $row['sectionName']; $sectionHeading = false; } if(!$sectionHeading) { echo '<tr><td><h1>' . $row['sectionName'] . '</h1><br />' . $row['sectionDescription'] .'</td></tr>'; $sectionHeading = true; } else { echo '<tr><td style="paddding-left:30px;">' . $row['pName'] .'</td></tr>'; } } It is untested so it may not work. Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/#findComment-502388 Share on other sites More sharing options...
btd1924 Posted March 27, 2008 Author Share Posted March 27, 2008 Works - but the products under the categories are not repeating. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/#findComment-502411 Share on other sites More sharing options...
wildteen88 Posted March 27, 2008 Share Posted March 27, 2008 After testing I realised it wasn't working properly. I have just spent the last 30minutes trying to understand why it wasn't working properly and it was because of a silly mistake on my part: <?php $query = "SELECT * FROM sections s, products p WHERE p.pSection=s.sectionID ORDER BY sectionOrder ASC"; $result = mysql_query($query); echo '<table border="1" cellpadding="5" cellspacing="2">'."\n"; $prev_section = null; while($row = mysql_fetch_assoc($sql_query_result)) { if($prev_section != $row['sectionName']) { $prev_section = $row['sectionName']; echo '<tr><td><h1>' . $row['sectionName'] . '</h1><br />' . $row['sectionDescription'] ."</td></tr>\n"; } echo '<tr><td style="padding-left:30px;">' . $row['pName'] ."</td></tr>\n"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/#findComment-502421 Share on other sites More sharing options...
btd1924 Posted March 27, 2008 Author Share Posted March 27, 2008 Works perfect. Thanks for the time and your help. I hope this helps someone else. I still don't understand this but I will research it and try and write my code in a similar fashion. Thanks Again. Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/#findComment-502458 Share on other sites More sharing options...
btd1924 Posted March 27, 2008 Author Share Posted March 27, 2008 One last thing, are there any good books or sites to learn to code this way? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/#findComment-502460 Share on other sites More sharing options...
discomatt Posted March 27, 2008 Share Posted March 27, 2008 Code which way? Efficiently? For the most part, it's logic combined with a thorough understanding of the language. Quote Link to comment https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/#findComment-502461 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.