Forum Moderators: coopster

Message Too Old, No Replies

Two queries on one table

         

lala

1:37 pm on Feb 17, 2009 (gmt 0)

10+ Year Member



This seems like an easy one, but I just can't figure it out. I have a simple table, and two queries on the same page. Each query looks for a different field to pull records from. Upon first opening the page, the entire list of records from the first query is displayed. The other query seems to be working fine, only displays results on string value selection. What am I missing?

The code:

$sub_type=$_POST["sub_type"];
$sub_type_e=$_POST["sub_type_e"];

$result = mysql_query("SELECT * from case_studies where sub_type = '$sub_type' ORDER BY name ASC ");
$result2 = mysql_query("SELECT * from case_studies where sub_type_e = '$sub_type_e' ORDER BY name ASC ");

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select id=ent name="sub_type_e" size="1" onchange="this.form.submit();" >
<option value="<?=$sub_type_e?>" selected><?php echo "<h4> $sub_type_e </h4>" ; ?></option>
<option value="Improv Comedy">Improv Comedy</option>
<option value="Sketch Comedy">Sketch Comedy</option>
<option value="Guest Speakers">Guest Speakers</option>
<option value="Corporate Comedy">Corporate Comedy</option>
<option value="Corporate Imposters">Corporate Imposters</option>
<option value="Emcee ¦ Host">Emcee ¦ Host</option>
<option value="Video Productions">Video Productions</option>
<option value="Corporate Events">Corporate Events</option>
</select>
</form>

<?php ($row = mysql_fetch_array($result2))?>
<table width="120" border=0 class="table" cellpadding="2">
<tr>
<td align="left">
<a href="<?=$row[5]?>"><?=$row[2]?></a>
</td>
</tr>
<?
while ($row = mysql_fetch_array($result2))
{
?>
<tr>
<td align="left">
<a href="<?=$row[5]?>"><?=$row[2]?></a>
</td>
</tr>
<?
}
?>
</table>
Training:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select id=train name="sub_type" size="1" onchange="this.form.submit();" >
<option value="<?=$sub_type?>" selected><?php echo "<h4> $sub_type </h4>" ; ?></option>
<option value="Communication">Communication</option>
<option value="Executive Coaching">Executive Coaching</option>
<option value="Interview Skills">Interview Skills</option>
<option value="Leadership">Leadership</option>
<option value="Mediation">Mediation</option>
<option value="Presentation">Presentation</option>
<option value="Sales ¦ Bus Dev">Sales ¦ Bus Dev</option>
<option value="Team Building">Team Building</option>
<option value="Workplace Productivity">Workplace Productivity</option>

</select>
</form>
<?php ($row = mysql_fetch_array($result))?>
<table width="120" border=0 class="table" cellpadding="2">
<tr>
<td align="left">
<a href="<?=$row[5]?>"><?=$row[2]?></a>
</td>
</tr>
<?
while ($row = mysql_fetch_array($result))
{
?>
<tr>
<td align="left">
<a href="<?=$row[5]?>"><?=$row[2]?></a>
</td>
</tr>
<?
}
?>
</table>

Any help is much appreciated.

trigoon

2:13 pm on Feb 17, 2009 (gmt 0)

10+ Year Member



These lines aren't necessary, I'm not sure if they're causing the problems but its worth a try:
<?php ($row = mysql_fetch_array($result2))?>
<?php ($row = mysql_fetch_array($result))?>

You should use a while loop when working with something that pulls multiple rows.

So try pushing up the while loop and replacing the above lines with it.

lala

4:38 pm on Feb 17, 2009 (gmt 0)

10+ Year Member



I'm getting there, but still when the page first loads, the first query ($result) returns all results from that field. The second query ($result2) only returns results, and the correct ones at that, upon form submit.

Any other ideas? Updated code:

$sub_type=$_POST["sub_type"];
$sub_type_e=$_POST["sub_type_e"];

$result = mysql_query("SELECT * from case_studies where sub_type = '$sub_type' ORDER BY name ASC ");
$result2 = mysql_query("SELECT * from case_studies where sub_type_e = '$sub_type_e' ORDER BY name ASC ");

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select id=ent name="sub_type_e" size="1" onchange="this.form.submit();" >
<option value="<?=$sub_type_e?>" selected><?php echo "<h4> $sub_type_e </h4>" ; ?></option>
<option value="Improv Comedy">Improv Comedy</option>
<option value="Sketch Comedy">Sketch Comedy</option>
<option value="Guest Speakers">Guest Speakers</option>
<option value="Corporate Comedy">Corporate Comedy</option>
<option value="Corporate Imposters">Corporate Imposters</option>
<option value="Emcee ¦ Host">Emcee ¦ Host</option>
<option value="Video Productions">Video Productions</option>
<option value="Corporate Events">Corporate Events</option>
</select>
</form>

<?php while($row=mysql_fetch_array($result2))
{
?>

<table width="120" border=0 class="table" cellpadding="2">
<tr>
<td align="left">
<a href="<?=$row[5]?>"><?=$row[2]?></a>
</td>
</tr>

<?
}
?>
</table>
Training:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select id=train name="sub_type" size="1" onchange="this.form.submit();" >
<option value="<?=$sub_type?>" selected><?php echo "<h4> $sub_type </h4>" ; ?></option>
<option value="Communication">Communication</option>
<option value="Executive Coaching">Executive Coaching</option>
<option value="Interview Skills">Interview Skills</option>
<option value="Leadership">Leadership</option>
<option value="Mediation">Mediation</option>
<option value="Presentation">Presentation</option>
<option value="Sales ¦ Bus Dev">Sales ¦ Bus Dev</option>
<option value="Team Building">Team Building</option>
<option value="Workplace Productivity">Workplace Productivity</option>

</select>
</form>

<?php while($row=mysql_fetch_array($result))
{
?>
<table width="120" border=0 class="table" cellpadding="2">
<tr>
<td align="left">
<a href="<?=$row[5]?>"><?=$row[2]?></a>
</td>
</tr>

<?
}
?>
</table>

Thanks again!