phpbeginner Posted January 24, 2012 Share Posted January 24, 2012 I have 3 tables (sectors, subsectors, and business). From the site a site user can select a sector, which then lists the subsectors, then when they select a sub sector they can view a list of the businesses within the selection. I am trying to remove a step so that they will see the sectors and be able to select, then will list the subsectors with the business names under the subsectors. The code I have is prior to removing the step is ...... <?php $rs=mysql_query("select * from tblmain WHERE id='$view'"); while($row=mysql_fetch_row($rs)){ foreach ($row as $k => $v){ $row[$k] = nl2br($v); } echo("<h4>" . $row[1] . " Sectors </h4>"); } ?> <ul> <?php $pnds=1; if (isset($_GET["id"])) $pnds=$_GET["id"]; $sql = "select * from tblsub WHERE catid='$view' ORDER BY subsec "; $reccount=$sq->numsrow($sql); if ($reccount > 0) { $ata =$sq->query($sql); while($rs=$sq->fetch($ata)) { ?> <li><a href="listings.php?view=<?php echo $rs["subsec"];?>"><?php echo $rs["subsec"];?></a></li> <?php } } ?> </ul> I have tried to get this to work but the problem I am having is $view is an number but the actual business table is not id, it is by subsector name. I have tried ....... <?php $rs=mysql_query("select * from tblmain WHERE id='$view'"); while($row=mysql_fetch_row($rs)){ foreach ($row as $k => $v){ $row[$k] = nl2br($v); } echo("<h4>" . $row[1] . " Sectors </h4>"); } ?> <ul> <?php $pnds=1; if (isset($_GET["id"])) $pnds=$_GET["id"]; $sql = "select * from tblsub WHERE catid='$view' ORDER BY subsec "; $reccount=$sq->numsrow($sql); if ($reccount > 0) { $ata =$sq->query($sql); while($rs=$sq->fetch($ata)) { ?> <li><a href="listings.php?view=<?php echo $rs["subsec"];?>"><?php echo $rs["subsec"];?></a></li> <?php $rs=mysql_query("select * from tblbusiness WHERE category='$view' ORDER BY name"); while($row=mysql_fetch_row($rs)){ foreach ($row as $k => $v){ $row[$k] = nl2br($v); } print(" . $row[2] . "); } <?php } } ?> </ul> But obviously category is by name and not $view which is an id. Any help would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/255672-basic-php-help/ Share on other sites More sharing options...
phpbeginner Posted January 24, 2012 Author Share Posted January 24, 2012 Latest effort........ <?php $rs=mysql_query("select name, category from tblbusiness ORDER BY name, tblsub WHERE subsec=category "); while($row=mysql_fetch_row($rs)){ foreach ($row as $k => $v){ $row[$k] = nl2br($v); } echo("" . $row[name] . ""); } ?> Link to comment https://forums.phpfreaks.com/topic/255672-basic-php-help/#findComment-1310614 Share on other sites More sharing options...
phpbeginner Posted January 24, 2012 Author Share Posted January 24, 2012 I am trying to now list the business names under each subsector...... Select from tblbusiness where category is equal to tblsub subsector Link to comment https://forums.phpfreaks.com/topic/255672-basic-php-help/#findComment-1310622 Share on other sites More sharing options...
phpbeginner Posted January 24, 2012 Author Share Posted January 24, 2012 I now have this much....it lists the subsectors but as you can see I just grabbed the info from tblbusiness WHERE category= 'Bed and Brekfast' while I am trying to grab from tblbusiness WHERE category = from tblsub WHERE catid='$view' <?php $pnds=1; if (isset($_GET["id"])) $pnds=$_GET["id"]; $sql = "select * from tblsub WHERE catid='$view' ORDER BY subsec "; $reccount=$sq->numsrow($sql); if ($reccount > 0) { $ata =$sq->query($sql); while($rs=$sq->fetch($ata)) { ?> <li><a href="listings.php?view=<?php echo $rs["subsec"];?>"><?php echo $rs["subsec"];?></a></li> <p> <?php $rs=mysql_query("SELECT * FROM tblbusiness WHERE category = 'Bed and Breakfast'"); while($row=mysql_fetch_row($rs)){ foreach ($row as $k => $v){ $row[$k] = nl2br($v); } echo("" . $row[2] . " | "); } ?> </p> <?php } } ?> Link to comment https://forums.phpfreaks.com/topic/255672-basic-php-help/#findComment-1310646 Share on other sites More sharing options...
phpbeginner Posted January 24, 2012 Author Share Posted January 24, 2012 My latest attempt, to no avail......... <?php $pnds=1; if (isset($_GET["id"])) $pnds=$_GET["id"]; $sql = "select * from tblsub WHERE catid='$view' ORDER BY subsec "; $reccount=$sq->numsrow($sql); if ($reccount > 0) { $ata =$sq->query($sql); while($rs=$sq->fetch($ata)) { ?> <li><a href="listings.php?view=<?php echo $rs["subsec"];?>"><?php echo $rs["subsec"];?></a></li> <p> <?php $rs=mysql_query("SELECT tblbusiness.category, tblsub.subsec FROM tblbusiness JOIN tblsub WHERE tblbusiness.category = tblsub.subsec AND tblsub.catid='$view'"); while($row=mysql_fetch_row($rs)){ foreach ($row as $k => $v){ $row[$k] = nl2br($v); } echo("" . $row[2] . " | "); } ?> </p> <?php } } ?> </ul> Link to comment https://forums.phpfreaks.com/topic/255672-basic-php-help/#findComment-1310667 Share on other sites More sharing options...
phpbeginner Posted January 24, 2012 Author Share Posted January 24, 2012 Here is something I just tried......the only thing is it lists all the subsector businesses under each subsector and did not sepearte them as per subsector. <?php $pnds=1; if (isset($_GET["id"])) $pnds=$_GET["id"]; $sql = "select * from tblsub WHERE catid='$view' ORDER BY subsec "; $reccount=$sq->numsrow($sql); if ($reccount > 0) { $ata =$sq->query($sql); while($rs=$sq->fetch($ata)) { ?> <li><a href="listings.php?view=<?php echo $rs["subsec"];?>"><?php echo $rs["subsec"];?></a></li> <p> <?php $rs=mysql_query("SELECT tblbusiness.*, tblsub.* FROM tblbusiness JOIN tblsub WHERE tblbusiness.category = tblsub.subsec AND tblsub.catid='$view'"); while($row=mysql_fetch_row($rs)){ foreach ($row as $k => $v){ $row[$k] = nl2br($v); } echo("" . $row[2] . " | "); } ?> </p> <?php } } ?> </ul> Link to comment https://forums.phpfreaks.com/topic/255672-basic-php-help/#findComment-1310687 Share on other sites More sharing options...
phpbeginner Posted January 24, 2012 Author Share Posted January 24, 2012 can anyone help with this? Link to comment https://forums.phpfreaks.com/topic/255672-basic-php-help/#findComment-1310754 Share on other sites More sharing options...
dzelenika Posted January 24, 2012 Share Posted January 24, 2012 You omitted $view = $_GET["view"]; Link to comment https://forums.phpfreaks.com/topic/255672-basic-php-help/#findComment-1310777 Share on other sites More sharing options...
phpbeginner Posted January 24, 2012 Author Share Posted January 24, 2012 Still shows all listings under each subsector. With my quesry I am trying to match the tblbusiness.category with the tblsub.subsec and also tblsub.catid = "$view". It displays only all lsitings as if it was simply tblsub.catid = "$view". Link to comment https://forums.phpfreaks.com/topic/255672-basic-php-help/#findComment-1310780 Share on other sites More sharing options...
phpbeginner Posted January 24, 2012 Author Share Posted January 24, 2012 My initial query works fine, displaying the results of subsectors. $sql = "select * from tblsub WHERE catid='$view' ORDER BY subsec "; However to list all the businesses under the correct subsector, I am using this.... $rs=mysql_query("SELECT tblbusiness.*, tblsub.* FROM tblbusiness JOIN tblsub WHERE tblbusiness.category = tblsub.subsec AND tblsub.catid='$view'"); As the common fld between the 2 tables is tblbusiness.category and tblsub.subsec Link to comment https://forums.phpfreaks.com/topic/255672-basic-php-help/#findComment-1310788 Share on other sites More sharing options...
dzelenika Posted January 24, 2012 Share Posted January 24, 2012 what about? $rs=mysql_query("SELECT tblbusiness.*, tblsub.* FROM tblbusiness JOIN tblsub WHERE tblbusiness.category = tblsub.subsec AND tblsub.catid='$view' AND tblsub.subsec = '" . $rs["subsec"] . "'"); Link to comment https://forums.phpfreaks.com/topic/255672-basic-php-help/#findComment-1310792 Share on other sites More sharing options...
phpbeginner Posted January 24, 2012 Author Share Posted January 24, 2012 YAY! It worked! Thanks for all your help, I appreciate it!! Link to comment https://forums.phpfreaks.com/topic/255672-basic-php-help/#findComment-1310793 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.