Jump to content

Nested Repeat Region Question


btd1924

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/98177-nested-repeat-region-question/
Share on other sites

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.

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.

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.

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
)

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.

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>";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.