Forum Moderators: coopster

Message Too Old, No Replies

If then help

         

adammc

11:30 pm on Sep 19, 2005 (gmt 0)

10+ Year Member



Hi folks,

I am currently using the code belwo on my job search site to display the jobs available in each category.

I was hoping to be able to use a if then / else statement but I am not sure how to do it :(

I want the script to use the call below, if there are less than 3 results in the query I want it to echo "code here"


<?php

$a = mysql_query("SELECT * FROM job_post WHERE JobCategory = 'Hospitality' order by job_id desc LIMIT 20");
while($b = mysql_fetch_array($a))
{
?>

jezra

11:35 pm on Sep 19, 2005 (gmt 0)

10+ Year Member



This should do the trick.
$a = mysql_query("YOUR QUERY");
if(mysql_num_rows($a) < 3 )
{
echo "code here";
}else{
//do some other stuff
}

adammc

11:54 pm on Sep 19, 2005 (gmt 0)

10+ Year Member



thanks for the quick reply :)

I tried using something like this but got errors:
Parse error: parse error, unexpected T_ELSE in /home/httpd/vhosts/workincairns.com/httpdocs/categories2.php on line 47

Line 47 = else {


<?php

$a = mysql_query("SELECT * FROM job_post WHERE JobCategory = 'Accounting' order by job_id desc LIMIT 20");
if(mysql_num_rows($a >= 3)) {
while($b = mysql_fetch_array($a)) {

?>
<tr align='center' bgcolor='#f2f2f2'>
<td width='15%'><font size='1' face='Verdana' color='black'>
<?=addslashes($b["postdate"])?>
</font></td>
<td width='45%'><strong>
<a href=http://<?=$myurl?>/jobseekers/job-info.php?job_id=<?=$b[job_id]?>><font size='1' face='Verdana' color='#000099'><?=addslashes($b["position"])?></a>
</strong></td>
<td width='20%'><font size='1' face='Verdana' color='black'>
<?=addslashes($b["JobIn"])?>
</font></td>
<td width='15%'><font size='1' face='Verdana' color='black'>
<?=addslashes($b["EXday"])?>
/
<?=addslashes($b["EXmonth"])?>
/
<?=addslashes($b["EXyear"])?>
</font></td>
</tr>

<?
}
else {

$a2 = mysql_query("SELECT * FROM job_post order by job_id desc LIMIT 27");
while($b2 = mysql_fetch_array($a2))
{
?>
<tr align='center' bgcolor='#f2f2f2'>
<td width='15%'><font size='1' face='Verdana' color='black'>
<?=addslashes($b2["postdate"])?>
</font></td>
<td width='45%'><strong>
<a href=http://<?=$myurl?>/jobseekers/job-info.php?job_id=<?=$b2[job_id]?>><font size='1' face='Verdana' color='#000099'><?=addslashes($b2["position"])?></a>
</strong></td>
<td width='20%'><font size='1' face='Verdana' color='black'>
<?=addslashes($b2["JobIn"])?>
</font></td>
<td width='15%'><font size='1' face='Verdana' color='black'>
<?=addslashes($b2["EXday"])?>
/
<?=addslashes($b2["EXmonth"])?>
/
<?=addslashes($b2["EXyear"])?>
</font></td>
</tr>
<?php
}
?>


<?php
}
?>
</table>

jezra

12:14 am on Sep 20, 2005 (gmt 0)

10+ Year Member



It would appear that you are missing the closing bracket for your "while" loop.

adammc

12:17 am on Sep 20, 2005 (gmt 0)

10+ Year Member



Ooops, thanks for pointing that out ;)

I added the bracket, the page loaded & the if else worked.. I still got an eror though :

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/workincairns.com/httpdocs/categories2.php on line 22

Is it a conflict between the $a & $a2 queries that are inside the same while statement?


<?php

$a = mysql_query("SELECT * FROM job_post WHERE JobCategory = 'Hospitality' order by job_id desc LIMIT 20");
if(mysql_num_rows($a >= 3)) {
while($b = mysql_fetch_array($a)) {
/* while loop stuff */
?>
<tr align='center' bgcolor='#f2f2f2'>
<td width='15%'><font size='1' face='Verdana' color='black'>
<?=addslashes($b["postdate"])?>
</font></td>
<td width='45%'><strong>
<a href=http://<?=$myurl?>/jobseekers/job-info.php?job_id=<?=$b[job_id]?>><font size='1' face='Verdana' color='#000099'><?=addslashes($b["position"])?></a>
</strong></td>
<td width='20%'><font size='1' face='Verdana' color='black'>
<?=addslashes($b["JobIn"])?>
</font></td>
<td width='15%'><font size='1' face='Verdana' color='black'>
<?=addslashes($b["EXday"])?>
/
<?=addslashes($b["EXmonth"])?>
/
<?=addslashes($b["EXyear"])?>
</font></td>
</tr>

<?

}
}
else {
/* less than 3 results */

$a2 = mysql_query("SELECT * FROM job_post order by job_id desc LIMIT 27");
while($b2 = mysql_fetch_array($a2))
{
?>
<tr align='center' bgcolor='#f2f2f2'>
<td width='15%'><font size='1' face='Verdana' color='black'>
<?=addslashes($b2["postdate"])?>
</font></td>
<td width='45%'><strong>
<a href=http://<?=$myurl?>/jobseekers/job-info.php?job_id=<?=$b2[job_id]?>><font size='1' face='Verdana' color='#000099'><?=addslashes($b2["position"])?></a>
</strong></td>
<td width='20%'><font size='1' face='Verdana' color='black'>
<?=addslashes($b2["JobIn"])?>
</font></td>
<td width='15%'><font size='1' face='Verdana' color='black'>
<?=addslashes($b2["EXday"])?>
/
<?=addslashes($b2["EXmonth"])?>
/
<?=addslashes($b2["EXyear"])?>
</font></td>
</tr>
<?php
}
}
?>

jezra

12:34 am on Sep 20, 2005 (gmt 0)

10+ Year Member



your if statement:
if(mysql_num_rows($a >= 3))
should be
if ( mysql_num_rows($a) >= 3 )

or to make it a bit more clear

$number_of_rows = mysql_num_rows($a);
if($number_of_rows >=3 )
{
//do stuff
}

adammc

12:44 am on Sep 20, 2005 (gmt 0)

10+ Year Member



It works a treat, thank you so much :)