/    Sign up×
Community /Pin to ProfileBookmark

need some help with a looping issue

Been working on this script for 5 days, got one issue fixed, but the solution has caused a new problem that I’ve been pulling my hair out over. When the Subcategory is showing, I’m trying to get all the selected data from from each listing that has the same Subcategory to be in the same column. Right now, it works fine as long as there is only one listing, but if there are 2 or more it puts the data in the next column.

Pretty sure I need to loop through the data until there are no more listings for that Subcategory. But I have tried a for loop, a foreach (which I’ve never been able to get working) and a nested while loop, all with funky results. Which is probably because I did something wrong. Not sure if I’m putting it in the wrong place or if I’m constructing it wrong, But I could sure use some help with this!

This is the code I think I need to loop through

[CODE]////Data to shown for each business listing of the same Subcategory if ($row[‘udURL’] == “”){ echo “<font face=”Verdana”>{$row[‘Bname’]}</font>”; }else{ echo “<a href=”http://{$row[‘udURL’]}”><font face=”Verdana”>{$row[‘Bname’]}</font></a>”; } echo “<br>”; if ($row[‘Slogan’] != ”) { echo “<font face=”Verdana” size=2><i>{$row[‘Slogan’]}</i></font><br>”; } if (($row[‘TFPhone’] != “”) && ($row[‘LPhone’] != “”)){echo “<font face=”Verdana” size=2>{$row[‘TFPhone’]} / {$row[‘LPhone’]}<br></font>”; }else{ echo “<font face=”Verdana” size=2>{$row[‘TFPhone’]} {$row[‘LPhone’]}<br></font>”; } if ($row[‘CLSmap’] == ‘Y’){ echo “<font face=”Verdana” size=2><a href=”maps/maps.php?IDENT={$row[‘ID’]}” target=”_blank”>Map/Directions</a></font>”; } ////END of data to be shown for each business with the same Subcategory[/CODE]

ENTIRE SCRIPT to show the current interactions (I have it online for testing at [URL=”http://www.citylifesites.com/services.php?CLSfolder=pinehurst-nc&City=Pinehurst&State=NC”]http://www.citylifesites.com/services.php?CLSfolder=pinehurst-nc&City=Pinehurst&State=NC[/URL]

[CODE]$last_subcategory = ”;
$last_category = ”; // initialize variable to detect a change in the category. Set to a value that will never exist as data
$columns = 3; // number of columns in a table row
$count = 0; // counter to track current column in the table row

$sub = mysql_query(“SELECT * FROM $DBTABLE WHERE (CLSfolder=’$selectedCLSfolder’) && (Active=’Y’) ORDER BY Category, Subcategory”);

echo “<table width=”98%” align=”center”>n”; // overall table (whatever you current have…)
while($row = mysql_fetch_array($sub)){

// check for a new Category and output the heading when the Category changes –
if($last_category != $row[‘Category’]){
// if $count is not 0, finish any previous partial row/section of Subcategory data
if($count != 0){
// finish the current row
while($count % $columns != 0){
echo “<td>&nbsp;</td>”;
$count++;
}
// close the row/section
echo “</tr>n</table>n”;
}
// output the Category heading
echo “<tr><td colspan=3>&nbsp;</td></tr><tr><td valign=”top” bgcolor=”#FDD9BA”>”;

echo “<font face=”Verdana”><b>{$row[‘Category’]}</b></font></td></tr>n”;
$last_category = $row[‘Category’]; // remember the new Category
$count = 0; // reset the count (new set of rows)
}
// at this point, you have Subcategory data to display
// if $count == 0, need to start a new section for the Subcategory data
if($count == 0){
echo “<tr><td><TABLE width=”100%” border=0 align=”center”>n<tr>”;
} elseif($count % $columns == 0) {
// if count is not 0 and $count % $columns == 0, need to finish previous row and start a new one
echo “</tr>n<tr>”;
}
// display the actual Subcategory data –
echo “<td width=”33%” align=”left” valign=”top”>”;
if ($row[‘Subcategory’] == ‘None’){
$row[‘Subcategory’]=”View All “.$row[‘Category’];}

if($last_subcategory != $row[‘Subcategory’]){

echo “<ul id=”{$row[‘Subcategory’]}” class=”treeview”>
<font face=”Verdana” size=2><li><b>{$row[‘Subcategory’]}</b></font><br><br>
<ul>”;
}

////Data to shown for each business listing of the same Subcategory
if ($row[‘udURL’] == “”){
echo “<font face=”Verdana”>{$row[‘Bname’]}</font>”;
}else{
echo “<a href=”http://{$row[‘udURL’]}”><font face=”Verdana”>{$row[‘Bname’]}</font></a>”;
}

echo “<br>”;
if ($row[‘Slogan’] != ”)
{
echo “<font face=”Verdana” size=2><i>{$row[‘Slogan’]}</i></font><br>”;
}
if (($row[‘TFPhone’] != “”) && ($row[‘LPhone’] != “”)){echo “<font face=”Verdana” size=2>{$row[‘TFPhone’]} / {$row[‘LPhone’]}<br></font>”;
}else{
echo “<font face=”Verdana” size=2>{$row[‘TFPhone’]} {$row[‘LPhone’]}<br></font>”;
}
if ($row[‘CLSmap’] == ‘Y’){
echo “<font face=”Verdana” size=2><a href=”maps/maps.php?IDENT={$row[‘ID’]}” target=”_blank”>Map/Directions</a></font>”;}

///END of data to be shown for each business with the same Subcategory

echo”</li></ul> </li></ul>”;

echo ”
<script type=”text/javascript”>
ddtreemenu.createTree(“{$row[‘Subcategory’]}”, false)
</script></td>
“;
$last_subcategory = $row[‘Subcategory’];
$count++; // count one Subcategory
} // end of while loop
// if $count is not 0, finish any previous partial row/section of Subcategory data
if($count != 0){
while($count % $columns != 0){
echo “<td>&nbsp;</td>”;
$count++;
}
// close the row/section
echo “</tr>n</table>”;
}
echo “</table>n”; // close overall table
[/CODE]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@bryan_5050Dec 14.2009 — 
  • 1. Create a separate table called sub-categories. In this table, have a cat_id and sub category name.


  • sub_cat_id = PRIMARY key; AUTO-INCREMENT

    cat_id

    sub_cat_title

  • 2. Run a query to get ALL categories that is selected and Active.

  • 3. Display category name and whatever else you wish to show from the category table.

  • 4. While inside this loop, run a query to see if any sub-categories are associated with the cat_id. If so, display them.
    [CODE]

    $sql = "SELECT * FROM category table WHERE WHERE (CLSfolder='$selectedCLSfolder') && (Active='Y')";
    $result = mysql_query($sql);

    while($row = mysql_fetch_assoc($result))
    {
    //Display category heading info
    $sql_sub = "SELECT * FROM sub_category WHERE cat_id = ".$row["cat_id"]." ORDER BY sub_cat_title";
    $result_sub = mysql_query($sql_sub);
    while($row_sub = mysql_fetch_assoc($result_sub))
    {
    echo the title.

    }



    }
    [/CODE]


    you will need to link the category table with the sub_category table so the WHERE cat_id = ".$row["cat_id"]." line works.



    If you want more info in the sub_category table, feel free to add it.



    Hope this helps.
  • Copy linkTweet thisAlerts:
    @stmosaicauthorDec 14.2009 — Brian,

    Thanks, I actually do have a category/subcategory table already. I will see if what you suggested will incorporate into the code I have. It has gotten so complex with the dynamic columns, the dynamic categories/subcategory script and the JS click-to-hide script that it has my eyes spinning.
    Copy linkTweet thisAlerts:
    @stmosaicauthorDec 15.2009 — After studying bryan's answer, it does not appear to do what I need. I have the Category and Subcategory showing as I would like, it's the records under each subcategory that I need to show directly under the Subcategory section. I've tried the following while loop. but all it does is hang up. I'm so frustrated with this.

    [CODE]////Data to shown for each business listing of the same Subcategory
    $current_sub = $row['Subcategory'];
    $sql_sub = mysql_query("SELECT * FROM $DBTABLE WHERE (CLSfolder='$selectedCLSfolder') && (Subcategory = '$current_sub') ORDER BY Bname");
    $result_sub = mysql_fetch_array($sql_sub);
    $csubcat = $result_sub['Subcategory'];
    while($csubcat = $current_sub){

    if ($result_sub['udURL'] == ""){
    echo "<font face="Verdana">{$result_sub['Bname']}</font>";
    }else{
    echo "<a href="http://{$result_sub['udURL']}"><font face="Verdana">{$result_sub['Bname']}</font></a>";
    }

    echo "<br>";
    if ($result_sub['Slogan'] != '')
    {
    echo "<font face="Verdana" size=2><i>{$result_sub['Slogan']}</i></font><br>";
    }
    if (($result_sub['TFPhone'] != "") && ($result_sub['LPhone'] != "")){echo "<font face="Verdana" size=2>{$result_sub['TFPhone']} / {$result_sub['LPhone']}<br></font>";
    }else{
    echo "<font face="Verdana" size=2>{$result_sub['TFPhone']} {$result_sub['LPhone']}<br></font>";
    }
    if ($result_sub['CLSmap'] == 'Y'){
    echo "<font face="Verdana" size=2><a href="maps/maps.php?IDENT={$result_sub['ID']}" target="_blank">Map/Directions</a></font>";}
    }//end of while loop
    ///END of data to be shown for each business with the same Subcategory
    [/CODE]
    ×

    Success!

    Help @stmosaic spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...