Jump to content

Basic PHP Help


phpbeginner

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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