Jump to content

how do i create if statements???


slaterino

Recommended Posts

Hi,

This is quite a basic query but always like to get people's opinions rather than using the manuals. Nothing quite like experience! Basically, I have a page which shows various fields from a table. I want it set so that there's a line break after every field. However, if there is no field I don't want the line break to appear. Can someone give a quick example of how this would be coded. This is the current code for the site:

 

$query  = "SELECT society, venue, dates, contactname, contactadd, contacttel, contactemail FROM showdates ORDER BY dates ASC";
$result = mysql_query($query);

while(list($society, $venue, $dates, $contactname, $contactadd, $contacttel, $contactemail)= mysql_fetch_row($result))
{
$date=$dates;
list($year,$month,$day)=split("-",$date);
$display_date=date("j M Y",mktime(0,0,0,$month,$day,$year));
echo "<b>$society</b><br />$venue<br />$display_date<br />$contactname<br />$contactadd<br />$contacttel<br />$contactemail";
}

 

I want this code to be used on all the fields that are being echo'd at the bottom.

 

Cheers,

Russ

Link to comment
https://forums.phpfreaks.com/topic/120271-how-do-i-create-if-statements/
Share on other sites

Why don't you just use $date in the first list() function instead of changing the name?

 

Anyway, to answer your question, it would be a lot easier if you left the variables in an array. Roughly:

while ($row = mysql_fetch_row($result))
{
   foreach ($row as $field)
   {
      if ($field)
         echo "<b>$field</b><br>";
   }
}

Hey I'm a relative newbie to php. How does the foreach ($row as $field) bit work. I'm presuming you mean for the rest to be something like:

 

$result = mysql_query($query);

while ($row = mysql_fetch_row($result))
{
foreach ($row as $field)
{
if ($society)
echo "<b>$society</b><br />";
if ($venue)
echo "<b>$venue</b><br />";
.... and so on
}
}

Slate - although you like to get the opinion of others it's actually only hindering your education twords a good educational background in programming - not to mention good housekeeping and syntax.

 

<?php
$result = mysql_query($query);

while($row = mysql_fetch_row($result)){
    foreach($row as $field){
if($society){
echo "<b>$society</b><br />";
}

if($venue){
echo "<b>$venue</b><br />";
}

?>

 

I fully understand that the example was for display purposes only but it's never a bad thing to use good programming housekeeping even while showing examples - it makes it much more easy for the experts on this board to better help you! :)

 

How does the foreach ($row as $field) bit work.

 

Like I said: http://php.net/ is a very good resource and if you have any questions like this it's best if you read a bit of the function description from php.net... Then ask any questions relevant to the information that you have obtained from them. :) But for the sake of humor I'll attempt to explain:

 

What you would like to do is actually very repetitive, so by using the while() and foreach() functions we are able to 'automate' your information.

 

<?php

while($row = mysql_fetch_row($result)){
   foreach($row as $field){
      if($field)
         echo "<b>$field</b><br>";
   }
}

?>

 

What this function will do is again like I said 'automate' your function depending on what $row is set to. :)

 

 

Hope this helps!

Hey I'm a relative newbie to php. How does the foreach ($row as $field) bit work. I'm presuming you mean for the rest to be something like:

No. In leaving the variables in an array you are able to loop through them without checking each one by name. In the code I posted, every time the while loop loops, $field has a different variable in it (first $society then $venue, etc..). Try replacing your loop with the one I posted and take a look at what it outputs.

Archived

This topic is now archived and is closed to further replies.

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