Jump to content

if else and while loops


ijyoung

Recommended Posts

I have a table which has names of club members against certain duties.Each member has a membership no identifiying them.

So table might look like :

id, event, date, ood,safety,ood_id,s_id.

I want to be able to pick out all duties for a given membership no.

query might look like.

$query = "SELECT *,DATE_FORMAT(date, '%a-%d-%m-%Y') AS dr from table where ood_id='$id' or  s_id='$id'" ;
 $result = @mysql_query ($query); // Run the query.
 if (!$result) {
    die("Query error! query text: $query<br />Error: " . mysql_error());
} 

 

Now I have tried two different if conditionals

$name = '';
  $duty = '';

  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if ($row['ood_id']==$id) {
        $name= $row['ood'];
        $duty='Race Officer';
        
    }

    if ($row['s_id']==$id) {
        $name= $row['safety'];
        $duty='Safety';
        
    }
//table here with $name $duty
}//end of while loop

and other try

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  if ($row['ood_id']==$id) {
  $name= $row['ood'];
  $duty='Race Officer';
  }elseif 
  ($row['s_id']==$id) {
  $name= $row['safety'];
  $duty='Safety';
}else{
  $name=FALSE;
  $duty=FALSE;
	}
//etc

 

Now the first conditional returns the last entry in the row and the second conditional returns the first record in the row.

 

How do I get all results per row?

 

I have been worrying away at this for some time and cannot see how to resolve.

 

Hope you can help

Link to comment
Share on other sites

Ok, sorry if not making myself clear.

 

I have tried two IF statements as above in two separate attempts to solve this problem.

In the row the fields would be something like.

Date:2008-10-05

Event: Race 5 of 6

OOD: Joe Bloggs

ood_id: 413

Safety:Jean Bloggs

S_id: 413.

 

This is much simplified as there are a number of other columns but this is the essence.

 

Using the ifelse version, Joe Bloggs and Race Officer, Race 5 of 6 2008-10-05 is returned

using the if version Jean Bloggs, Safety, Race 5 of 6 2008-10-05 is returned.

 

I need both to be returned. The search parameter is th $id.

 

Hope I have made myself clearer.

 

Cheers

 

Ian

 

Link to comment
Share on other sites

What about

 

if ($row['ood_id']==$id) {
        $ood_name= $row['ood'];
        $ood_duty='Race Officer';
        
    }

    if ($row['s_id']==$id) {
        $s_name= $row['safety'];
        $s_duty='Safety';
        
    }

 

Link to comment
Share on other sites

Now we are talking. Naturally that works fine.

 

I need to limit the results to those that are actully retunred otherwise blank rows.

 

I had used a function to build the results. Now not delivering!

while ($row = mysql_fetch_array($result)) {
  
    	if ($row['s2_id']==$id) {
        $name3= $row['safety2'];
        $duty3='Safety2';
result-duty();
	//etc
        
    }
}//end of while loop

function result-duty() {
echo "<table>";
    echo "<tr>";
echo "<td>Name: </td>";
echo "<td>".$name."</td>";

echo "</table>";

   
} 

 

The idea is that only those results that meet the conditions would be met. However, as $name is outside the loop it ain't working. As I said had this working before (deleted the test page) and cannot for the life of me see where I am going wrong

Link to comment
Share on other sites

Got it. Great what happens when you go away and come back to a problem.

Basically have just put a table in each of the conditions as per

    $name2= $row['safety'];
        $duty2='Safety';
	?>
	<table width="40%" align="center" bgcolor="red">
    <tr>	   
	<td><?  echo $row['dr']?></td>
	</tr>
	<tr>
	<td><? echo $row['event']?></td>
	</tr>
	<tr>
	<td>Name: 
	<? echo $name2?></td>
	<tr>

	<td>Duty: 
	<? echo $duty2?></td>
	</tr>
	</table>
	<?
        
        
    }

 

Now there may be a more elegant way of doing this but a least this works.

Thanks folks.

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.