Jump to content

If / Else Multiple conditions? Is this possible?


Skipjackrick

Recommended Posts

Ok, So I am trying to set some if/else statements with several conditions and I am getting a fatal error.

 

Example,

 

IF num_rows = 3  {execute code 1} 

 

else { IF num_rows = 4 {execute code 2} }

 

else { {execute code 3} }

 

}

 

Anyways, maybe its possible....here is what I've got so far..

 

<?php


$querymembers = "SELECT 
		*
		FROM anglers
		WHERE team_id=$teamvar
		GROUP BY angler
		LIMIT 5";
$teamanglers = mysql_query($querymembers) or die(mysql_error());

if (mysql_num_rows($teamanglers) = 3)	{
while($row = mysql_fetch_assoc($teamanglers)) 
{
$results[] = $row;
}
$results[3]['angler'] = 0;
$results[3]['handle'] = vacant;
$results[3]['name'] = vacant;
$results[3]['kayak'] = vacant;
$results[4]['angler'] = 0;
$results[4]['handle'] = vacant;
$results[4]['name'] = vacant;
$results[4]['kayak'] = vacant;
} 
else 
{

if (mysql_num_rows($teamanglers) = 4)	{
while($row = mysql_fetch_assoc($teamanglers)) 
{
$results[] = $row;
}
$results[4]['angler'] = 0;
$results[4]['handle'] = vacant;
$results[4]['name'] = vacant;
$results[4]['kayak'] = vacant;
} 
else 
{ while($row = mysql_fetch_assoc($teamanglers)) 
{
$results[] = $row;
}
}
}

?>

 

Link to comment
Share on other sites

maybe your looking for something like this

<?php
if($condition1){
  //condition1
}elseif($condition2){
  //condition2
}else{
  //other
}
?>

 

or look into switch statement

 

 

 

I still get a fatal error and This is the only code I typed into the page.  Is this a syntax issue or something else?  Not sure?

 

Fatal error: Can't use function return value in write context in

 

<?php
$querymembers = "SELECT 
         *
         FROM anglers
         WHERE team_id=$teamvar
         GROUP BY angler
         LIMIT 5";
$teamanglers = mysql_query($querymembers) or die(mysql_error());

if (mysql_num_rows($teamanglers)=3)
{
while($row = mysql_fetch_assoc($teamanglers)) 
{
$results[] = $row;
}
$results[3]['angler'] = 0;
$results[3]['handle'] = 'vacant';
$results[3]['name'] = 'vacant';
$results[3]['kayak'] = 'vacant';
$results[4]['angler'] = 0;
$results[4]['handle'] = 'vacant';
$results[4]['name'] = 'vacant';
$results[4]['kayak'] = 'vacant';
}
elseif(mysql_num_rows($teamanglers)=4){
while($row = mysql_fetch_assoc($teamanglers)) 
{
$results[] = $row;
}
$results[4]['angler'] = 0;
$results[4]['handle'] = 'vacant';
$results[4]['name'] = 'vacant';
$results[4]['kayak'] = 'vacant';
}
else 
{ while($row = mysql_fetch_assoc($teamanglers)) 
{
$results[] = $row;
}
}
?>
[code]

Link to comment
Share on other sites

As should the next if statement ;)

 

I just don't understand why you're pulling stuff from a database, and then setting the result array to another value:

$results[3]['angler'] = 0;

 

Well, its a fix for the rest of my code in the page.

 

If there are only three rows in the query then I get.

 

$results[0]['angler'] = 1

$results[1]['angler'] = 2

$results[2]['angler'] = 3

 

But I need to set the following variables to something otherwise it leaves my other queries empty and that won't work.....Therefore...I just set them as some other value that will make do.

$results[3]['angler'] = 0

$results[4]['angler'] = 0

 

Link to comment
Share on other sites

or just format the script abit. Current layout is hard to read.

<?php
if($var == $var2)
{
while($row = mysql_fetch_array($q))
{
// do something
}
}

 

Just format it something like this....

<?php
if($var == $var2)
{
    while($row = mysql_fetch_array($q))
    {
        // do something
    }
}

Link to comment
Share on other sites

Something like:

<?php
// query
$querymembers = "SELECT 
         *
         FROM `anglers`
         WHERE `team_id`='$teamvar'
         GROUP BY `angler`
         LIMIT 5";
$teamanglers = mysql_query($querymembers) or die(mysql_error());

// get count of how many rows
$rowCount = mysql_num_rows($teamanglers);

// let's get the results into an array
while($row = mysql_fetch_assoc($teamanglers))
$results[] = $row;

// what do we have?
switch($rowCount) {
case 3:
	$results[3]['angler'] = 0;
	$results[3]['handle'] = 'vacant';
	$results[3]['name'] = 'vacant';
	$results[3]['kayak'] = 'vacant';
	// since you wanted if 3 rows to set [4] as well, then we won't place a break here.
case 4:
	$results[4]['angler'] = 0;
	$results[4]['handle'] = 'vacant';
	$results[4]['name'] = 'vacant';
	$results[4]['kayak'] = 'vacant';
break;
default: 
	// do nothing I suppose
break;
}
?>

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.