simplish if else statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

johnworf
Forum Commoner
Posts: 28
Joined: Fri Nov 02, 2007 1:02 pm

simplish if else statement

Post by johnworf »

Hi,

i just want to display a simple HTML message...

<p>Do you want to advertise your restaurant in the town of Alabaster?</p>

..without using the table if there are no records - at the moment i'm obviously getting the table column headings and the table when there are no records

The number of records returned is $totalRows_Recordset1

so i know you have to do an if else constraint

so

Code: Select all

<?php if ($totalRows_Recordset1 > 0)
The trouble is i'm a bit stuck after that...where do i put the curly braces etc?

i thought i could just encapsulate all the code but i keep getting errors...

i managed to do a few lines...

such as

Code: Select all

echo "<table width=580 border=0 cellspacing=0 cellpadding=0>";
but i then get stuck at:

Code: Select all

<?php do {  ?>
also if you leave "0" speech marks around numbers it screws it up - how do you avoid that?

Code: Select all

<table width="580" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><h2>Restaurant</h2></td>
            <td><h2>Description</h2></td>
            <td><h2>Report</h2></td>
          </tr>
          <?php do {  ?>
          <tr <?php 
// technocurve arc 3 php bv block2/3 start
echo " style=\"background-color:$color\"";
// technocurve arc 3 php bv block2/3 end
?>>
            <td><a href="http://<?php echo $row_Recordset1['Website']; ?>" target="_blank" rel="nofollow"><?php echo $row_Recordset1['Restaurant']; ?></a></td>
            <td><?php echo $row_Recordset1['Description']; ?></td>
            <td><a href="mailto:reportabuse@foobar.com?subject=<?php echo $row_Recordset1['URL']; ?>">Report Abuse</a></td>        
          </tr>
          <?php 
// technocurve arc 3 php bv block3/3 start
if ($color == $color1) {
    $color = $color2;
} else {
    $color = $color1;
}
// technocurve arc 3 php bv block3/3 end
?>
            <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
        </table>
        <p>&nbsp;</p>
        <p>Do you want to <a href="../advertise-your-restaurant.php">advertise your restaurant</a> in the town of Alabaster?</p>
 
okay thanks for helping a newbiesh
Last edited by johnworf on Tue Feb 17, 2009 5:09 am, edited 5 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: simplish if else statement

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
johnworf
Forum Commoner
Posts: 28
Joined: Fri Nov 02, 2007 1:02 pm

Re: simplish if else statement

Post by johnworf »

i don't understand i've put those codes around it and it isn't showing on a white background - what have i done wrong - i read the article on posting code and it didn't mention it?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: simplish if else statement

Post by Mark Baker »

Should be

Code: Select all

$a = 1;
You have a space before the slash in your closing tag.
[ /code]

Code: Select all

$a = 1;[ /code]
johnworf
Forum Commoner
Posts: 28
Joined: Fri Nov 02, 2007 1:02 pm

Re: simplish if else statement

Post by johnworf »

ok thanks
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: simplish if else statement

Post by mattpointblank »

You don't need the 'do' part. Use something like:

Code: Select all

 
if($numRows > 0) {
 
    // display table stuff
 
} else {
 
    // tell them there's no rows
 
}
 
As for "0" breaking the code, it's because in the echo statement, it assumes you're closing the statement with double quotes, eg:

echo "hello";
echo "hello, "matt"; // breaks

You have to 'escape' them using a backslash:

echo "hello \"matt\", nice to meet you!";

Or, use single quotes for the echo statement:

echo 'hello "matt", nice to meet you!';

This approach means variables will display literally, eg $name will display as $name, not 'matt' or whatever.
johnworf
Forum Commoner
Posts: 28
Joined: Fri Nov 02, 2007 1:02 pm

Re: simplish if else statement

Post by johnworf »

Hi,

So i do that and that's where my trouble begins - if i simply shove my existing code (which works by the way) i start getting all sorts of errors - i can add all the echo for the table rows etc.etc. but i've no idea what to do about <?php do { ?> onwards

Code: Select all

 
<?php if ($totalRows_Recordset1>0) {
 
 <table width="580" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><h2>Restaurant</h2></td>
            <td><h2>Description</h2></td>
            <td><h2>Report</h2></td>
          </tr>
          <?php do {  ?>
          <tr <?php 
// technocurve arc 3 php bv block2/3 start
echo " style=\"background-color:$color\"";
// technocurve arc 3 php bv block2/3 end
?>>
            <td><a href="http://<?php echo $row_Recordset1['Website']; ?>" target="_blank" rel="nofollow"><?php echo $row_Recordset1['Restaurant']; ?></a></td>
            <td><?php echo $row_Recordset1['Description']; ?></td>
            <td><a href="mailto:reportabuse@foobar.com?subject=<?php echo $row_Recordset1['URL']; ?>">Report Abuse</a></td>        
          </tr>
          <?php 
// technocurve arc 3 php bv block3/3 start
if ($color == $color1) {
    $color = $color2;
} else {
    $color = $color1;
}
// technocurve arc 3 php bv block3/3 end
?>
            <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
        </table>
        }
        else
        {
        <p>&nbsp;</p>
        <p>Do you want to <a href="../advertise-your-restaurant.php">advertise your restaurant</a> in the town of Alabaster?</p>
        }
        ?>
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: simplish if else statement

Post by mattpointblank »

You don't need all that do/while stuff. Also, this is wrong:

Code: Select all

 
<?php if ($totalRows_Recordset1>0) {
 
 <table width="580" border="0" cellspacing="0" cellpadding="0">
 
You have to either close PHP to output your HTML, or echo it out:

Code: Select all

 
<?php if ($totalRows_Recordset1>0) { ?>
 
 <table width="580" border="0" cellspacing="0" cellpadding="0">
 
or

Code: Select all

 
<?php if ($totalRows_Recordset1>0) {
 
echo '<table width="580" border="0" cellspacing="0" cellpadding="0">';
 
johnworf
Forum Commoner
Posts: 28
Joined: Fri Nov 02, 2007 1:02 pm

Re: simplish if else statement

Post by johnworf »

hi,

yes i know the echo stuff is wrong - that's what i'm trying to tell you - i can do the table bit but it starts going all wrong at the do while loop

- but i do need all that do while stuff..otherwise it won't do what i want it to do...unless you can provide a new solution....i'm using dreamweaver and adding bits of code to it...

i'll give you the whole page...

Code: Select all

 
<?php
// technocurve arc 3 php bv block1/3 start
$color1 = "#99cc33";
$color2 = "#ccff99";
$color = $color1;
// technocurve arc 3 php bv block1/3 end
?><?php require_once('../Connections/myconn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
mysql_select_db($database_myconn, $myconn);
$query_Recordset1 = "SELECT Restaurant, Website, `Description`, URL FROM Alabama WHERE URL = 'http://www.foobar.com/Alabama/alabaster.php' ORDER BY Restaurant ASC";
$Recordset1 = mysql_query($query_Recordset1, $myconn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Alabaster Restaurants</title>
<meta name="keywords" content="Alabaster Restaurants" />
<meta name="description" content="Find a restaurant inAlabaster;the US state of Alabama Why not add your restaurant to the list?" />
<link href="../style/usa.css" rel="stylesheet" type="text/css" />
</head>
<body>
<!-- start header -->
<div id="header">
    <div id="logo">
      <h1><a href="#">foobar.com</a></h1>
  </div>
<div id="menu">
        <ul>
            <li><a href="http://www.foobar.com/index.html">Home</a></li>
            <li><a href="http://www.foobar.com/restaurant-articles/index.php">Restaurant Articles</a></li>
            <li><a href="http://www.foobar.com/recipes/index.php">Recipes</a></li>
        </ul>
    </div>
</div>
<div id="headerbg" align="center"><span style="white-space: nowrap" margin-top: "20px"><img src="../state-images/alabama.jpg" alt="state of alabama"><img src="../recipes/images/main-meals.jpg" width="450" height="209" /></span></div>
<div id="page">
    <!-- start content -->
    <div id="content">
        <!-- start latest-post -->
        <div id="latest-post" class="post">
            <h1 class="title">Alabaster Restaurants</h1>
<div class="entry">
 <?php if ($totalRows_Recordset1>0) {
 
 <table width="580" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><h2>Restaurant</h2></td>
            <td><h2>Description</h2></td>
            <td><h2>Report</h2></td>
          </tr>
          <?php do {  ?>
          <tr <?php 
// technocurve arc 3 php bv block2/3 start
echo " style=\"background-color:$color\"";
// technocurve arc 3 php bv block2/3 end
?>>
            <td><a href="http://<?php echo $row_Recordset1['Website']; ?>" target="_blank" rel="nofollow"><?php echo $row_Recordset1['Restaurant']; ?></a></td>
            <td><?php echo $row_Recordset1['Description']; ?></td>
            <td><a href="mailto:reportabuse@foobar.com.com?subject=<?php echo $row_Recordset1['URL']; ?>">Report Abuse</a></td>        
          </tr>
          <?php 
// technocurve arc 3 php bv block3/3 start
if ($color == $color1) {
    $color = $color2;
} else {
    $color = $color1;
}
// technocurve arc 3 php bv block3/3 end
?>
            <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
        </table>
        }
        else
        {
        <p>&nbsp;</p>
        <p>Do you want to <a href="../advertise-your-restaurant.php">advertise your restaurant</a> in the town of Alabaster?</p>
        }
        ?>
  </div>
      </div>
    <!-- end latest-post -->
        <!-- start recent-posts -->
        <div id="recent-posts">
            <div class="post">
                <h2 class="title">Restaurant Articles</h2>
                <p>Buying A Restaurant</p>
              <div class="entry">
            <p><a href="../restaurant-articles/buying-a-restaurant.php" class="more">Read more</a></p>
              </div>
            </div>
            <div class="post">
                <h2 class="title">Recipe</h2>
                <p>Cajun Chicken Wings</p>
              <div class="entry">
            <p><a href="http://www.foobar.com.com/recipes/cajun-chicken-wings.php" class="more">Read more</a></p>
              </div>
            </div>
            <div class="post">
                <h2 class="title">Business Plan</h2>
                <p>Seafood Restaurant</p>
              <div class="entry">
            <p><a href="http://www.foobar.com.com/business-plans/Seafood-Restaurant-Business-Plan.html" class="more">Read more</a></p>
              </div>
            </div>
            </div>
        <!-- end recent-posts -->
    </div>
    <!-- end content -->
    <!-- start sidebar -->
    <div id="sidebar">  
      <div style="clear: both;">&nbsp;</div>
    </div>
  <!-- end sidebar -->
</div>
<!-- end page -->
<div id="footer">
    <p id="legal">&copy;2008 All Restaurants USA. All Rights Reserved.</p>
  <p id="links"><a href="http://www.foobar.com/index.php">Home</a>&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp; <a href="http://www.foobar.com/terms.php">Terms</a> &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp; <a href="http://www.foobar.com/faq.php">FAQ</a> &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="http://www.foobar.com/contact.php">Contact Us</a></p>
</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: simplish if else statement

Post by mattpointblank »

Re-read my posts. You're mixing logic together which doesn't work and outputting code that isn't valid php because you're not closing the tags. Understand the basic structure first, build a test page that just outputs a basic message to satisfy each of your conditions, then when that works, add in the more complex code.
johnworf
Forum Commoner
Posts: 28
Joined: Fri Nov 02, 2007 1:02 pm

Re: simplish if else statement

Post by johnworf »

i've tried all this...so i've implemented the simple code and that displays fine

Code: Select all

 
<?php 
 
if ( $totalRows_Recordset1 > 0 ) {
    echo '<table width="580" border="0" cellspacing="0" cellpadding="0"><tr><td><h2>Restaurant</h2></td>  <td><h2>Description</h2></td><td><h2>Report</h2></td></tr></table>';
    
    echo '<p>Do you want to <a href="../advertise-your-restaurant.php">advertise your restaurant</a> in the town of Alabaster?</p>';
} else {    
    echo '<p>Do you want to <a href="../advertise-your-restaurant.php">advertise your restaurant</a> in the town of Alabaster?</p>';
}
?>
 
so that works fine..but of course it's completely useless...

i need to add in...

Code: Select all

 
<?php do {  ?>
          <tr <?php 
// technocurve arc 3 php bv block2/3 start
echo " style=\"background-color:$color\"";
// technocurve arc 3 php bv block2/3 end
?>>
            <td><a href="http://<?php echo $row_Recordset1['Website']; ?>" target="_blank" rel="nofollow"><?php echo $row_Recordset1['Restaurant']; ?></a></td>
            <td><?php echo $row_Recordset1['Description']; ?></td>
            <td><a href="mailto:reportabuse@foobar.com?subject=<?php echo $row_Recordset1['URL']; ?>">Report Abuse</a></td>        
          </tr>
          <?php 
// technocurve arc 3 php bv block3/3 start
if ($color == $color1) {
    $color = $color2;
} else {
    $color = $color1;
}
// technocurve arc 3 php bv block3/3 end
?>
            <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
        </table>
 
but i'm just not good enough to do it!
I'm sure it's a doddle
johnworf
Forum Commoner
Posts: 28
Joined: Fri Nov 02, 2007 1:02 pm

Re: simplish if else statement

Post by johnworf »

okay moved on a bit...

Parse error: syntax error, unexpected T_ECHO, expecting T_WHILE in...error in line 4

Code: Select all

 
do 
echo '<tr style="background-color:$color">';
echo '<td><a href="http://$row_Recordset1['Website']" target="_blank" rel="nofollow"><$row_Recordset1['Restaurant']</a></td>';
echo "<td><$row_Recordset1['Description']</td>";
echo '<td><a href="mailto:reportabuse@foobar.com?subject=$row_Recordset1['URL']">Report Abuse</a></td></tr>';
 
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
 
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: simplish if else statement

Post by mattpointblank »

Everything you put inside an echo statement will appear:

Code: Select all

 
echo '<td><a href="' . $row_Recordset1['Website'] . '" target="_blank" rel="nofollow">' . $row_Recordset1['Restaurant'] . '</a></td>';
 
When you're using array variables like these, you have to concatenate them into your echo, which means closing the quote marks (') and joining the echos together using dots, eg:

echo 'hello, my name is ' . $row['name'] . ', and I like php';

If they were normal variables (eg not arrays) you could just do:

echo 'hello, my name is $name';

One way round this is to define them as variables before echoing, eg:

$name = $row['name'];
echo "hello, my name is $name";

Which makes for more readable (albeit lengthy) code.
johnworf
Forum Commoner
Posts: 28
Joined: Fri Nov 02, 2007 1:02 pm

Re: simplish if else statement

Post by johnworf »

hi,

i keep getting this error...

Parse error: syntax error, unexpected T_ECHO, expecting T_WHILE in line 2

Code: Select all

 
echo '<td><a href="http://' .$row_Recordset1['Website']. '" target="_blank" rel="nofollow"><' .$row_Recordset1['Restaurant']. '</a></td>"<td>' .<$row_Recordset1['Description'].'</td>"';
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: simplish if else statement

Post by mattpointblank »

Can you post the code above that? It's more likely causing the problem.
Post Reply