Jump to content

looping issue in some complex code


stmosaic

Recommended Posts

Been working on this script for 5 days, got the duplicate Subcategory display 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. 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!

 

Code I need to loop through

////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

 

My for loop attempt:

	if(($last_subcategory != '') && ($last_subcategory == $row['Subcategory'])){
  $numsubcat = mysql_query("SELECT * FROM $DBTABLE WHERE (Subcategory = '$last_subcategory') && (Active='Y')");
  $ubname = mysql_query("SELECT Bname, udURL, SLogan, TFPhone, LPhone, CLSmap, ID FROM $DBTABLE WHERE (Subcategory = '$last_subcategory') && (Active='Y') ORDER BY Subcategory, Bname"); 
}


$subrow = mysql_fetch_array($ubname);
for ($srow = 0; $srow < $numsubcat; $srow++)
{//listing code}

 

The code as it exists right now (you can see how it is displaying at http://www.citylifesites.com/services.php?CLSfolder=pinehurst-nc&City=Pinehurst&State=NC)

$selectedCLSfolder=$_GET['CLSfolder'];
$selectedCity=$_GET['City'];
$selectedState=$_GET['State'];

$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> </td>";
			$count++;
		}
		// close the row/section
		echo "</tr>\n</table>\n";
	}
	// output the Category heading
	echo "<tr><td colspan=3> </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 "
</td>	<script type=\"text/javascript\">
ddtreemenu.createTree(\"{$row['Subcategory']}\", false)
</script>
";
$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> </td>";
	$count++;
}
// close the row/section
echo "</tr>\n</table>";
}
echo "</table>\n"; // close overall table

Link to comment
https://forums.phpfreaks.com/topic/185124-looping-issue-in-some-complex-code/
Share on other sites

viewed your source quickly and it appears you are actually putting them into separate columns with HTML markup:

 

<td width="33%" align="left" valign="top"><ul id="Tires" class="treeview">
<font face="Verdana" size=2><li><b>Tires</b></font><br><br>
<ul><a href="http://www.jasonstire.com"><font face="Verdana">Jason's Tire & Auto</font></a><br><font face="Verdana" size=2> (910) 944-3510  <br></font><font face="Verdana" size=2><a href="maps/maps.php?IDENT=92" target="_blank">Map/Directions</a></font></li></ul>	</li></ul>
</td>	<script type="text/javascript">
ddtreemenu.createTree("Tires", false)
</script>
<td width="33%" align="left" valign="top"><a href="http://www.thomastire.com"><font face="Verdana">Thomas Tire</font></a><br><font face="Verdana" size=2> (910) 944-6061<br></font><font face="Verdana" size=2><a href="maps/maps.php?IDENT=93" target="_blank">Map/Directions</a></font></li></ul>	</li></ul>

</td>

 

just a snippet of your code, but you can see the:

 

<td width="33%" align="left" valign="top">

 

before each listing/entry.  this creates a new column for each.

 

just correct your HTML accordingly and they should display one below the other.

Mr. Marcus, thank you for responding!

 

That's part of what is giving me a fit. I want every new Subcategory to be in a new column, but for a Subcategory that has 2 or more listings, they need to be in just one column.

 

The html part is correct in that it gives me a new column per subcategory when there is just one listing, which is what I want. It's when there is more than one listing per Subcategory that is the problem.

 

That's why I posted the part of the code that I think I need to loop through to accomplish this. Any other suggestions?

Still working on this bear. I've reworked my FOR loop, but it still does not display the data. I know I've done something wrong, but I can't see what it is.

 

$current_sub = $row['Subcategory'];
$sql_sub = mysql_query("SELECT * FROM $DBTABLE WHERE (CLSfolder='$selectedCLSfolder') && (Active='Y')  && (Subcategory = '$current_sub') ORDER BY Bname");
$result_sub = mysql_fetch_array($sql_sub);
for ($result_sub = 0; $result_sub < $sql_sub; $result_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 for loop
///END of data to be shown for each business with the same Subcategory

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.