Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with conditional statement

Status
Not open for further replies.

pugs421

Technical User
Joined
Nov 25, 2002
Messages
114
Location
US
I just want to display a row only if the result is not null. When I process the page It doesn't display the table rows at all. I am new to php and I am using Dreamweaver to generate the code. Based on previous threds I know this is not the recommended way among the hand coders but I am far from knowledgeable enough to hand code myself. I did buy a book and i am slowly learning. Thanks.

The only code I changed is the code after the body tag.

<?php
//Connection statement
require_once('../Connections/connupdate.php');

// begin Recordset
$colname__rssetup = '-1';
if (isset($HTTP_GET_VARS['user_name'])) {
$colname__rssetup = $HTTP_GET_VARS['user_name'];
}
$query_rssetup = sprintf(&quot;SELECT * FROM setup WHERE user_name = '%s'&quot;, $colname__rssetup);
$rssetup = $connupdate->SelectLimit($query_rssetup) or die($connupdate->ErrorMsg());
$totalRows_rssetup = $rssetup->RecordCount();
// end Recordset
//PHP ADODB document - made with PHAkt 2.4.0?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>
<?php
echo &quot;<table width='50%' border='0' cellspacing='0' cellpadding='0'>&quot;;
if ( $mobo != &quot;&quot; )
{
echo &quot;<tr><td>motherboard</td><td>$rssetup->Fields('mobo');</td></tr>&quot;;
}
if ( $ram != &quot;&quot; )
{
echo &quot;<tr><td>ram</td><td>$rssetup->Fields('ram');</td></tr>&quot;;
}
if ( $video_card != &quot;&quot; )
{
echo &quot;<tr><td>video card</td><td>$rssetup->Fields('video_card');</td></tr>&quot;;
}
?>
</table>
</body>
</html>
<?php
$rssetup->Close();
?>


 
It's nearly impossible to say what is wrong.

You're using a class definition from and included script, so I cannot say what might be happening inside that class.

You are referencing

$rssetup = $connupdate->SelectLimit($query_rssetup)

But I don't see where you initialize $connupdate.

Want the best answers? Ask the best questions: TANSTAAFL!
 
DW seems to count the results for you:
Code:
$totalRows_rssetup = $rssetup->RecordCount();

All you need now is the condition:
Code:
<?php
if ($totalRows_rssetup > 0){
   echo &quot; whatever HTML&quot;;
} else {
   echo &quot;Sorry, there were no results kin'a thing.&quot;;
}
?>[\code]

There's nothing wrong with doing the &quot;Hello world&quot; approach of learning.
DW might suffice as a quick fix if you need something urgently. But if you want to really learn PHP get away from DW asap.

I suppose anyone in this forum will be sharing their knowledge more readily if you post your own code. Who want's to read through Macromedia canned code? Yuck.
 
Was my if then statement set up correctly though? I'm trying to relate the code in the php book to the code of Dreamweaver and just make the changes I need in order to suit my needs. I know thats looked down upon in the hand coder community but it seems like a good way to learn to me.

I am having trouble identifying the different elements in the code in relation to what im reading in the book.(ex. &quot;Where's the arrays, functions, constants, etc.?&quot;) If any one wants to help me out and point out some of the different things going on in this code I'm sure it would help me uderstand PHP better. Thanks
 
pugs421,

your if statements are syntactically fine.

Example for an array in your code:
$HTTP_GET_VARS['user_name'] is an array element. $HTTP_GET_VARS is a special array, namely a superglobal array. This means that the elements are available anwhere in your code, even within functions.

What's an array? It's a collection of items that are stored within an indexed let's visualize it as a 'table': the first column is the index, the second column the value. Each row is called an element. The index can be numeric or a string value, in above example the index key is 'user_name'. Arrays with string keys are called associative.

The code that dreamweaver uses is quite advanced. The one line that includes an external PHP file
Code:
require_once('../Connections/connupdate.php');
includes probably a class for connecting to the database. Classes are quite advanced and come from object oriented programming.

Objects are an instance of a class. They contain variables and functions that are defined within the class. A function within a class is called a method. To invoke a method for an object the syntax you see is
Code:
$object->method($arguments);

Just from these few paragraphs you might see that the code that DW creates is everything else than easy to understand or a good example to learn from.

You really want to learn how to connect to a database server yourself, how to select a database, make a query and print out the results.

You can achieve the same thing that DW prepares for you with a few commands. It seems important though that you don't leave these commands to the canned DW code.

Ask yourself: What do i want the page to do? If you are able to write the pseudo-op code, you can write PHP code. There's no secret to it. And if you need help, there are many folks in this forum that are generous with advice.
 
Thanks for the response. I was wondering why the code looked so different than what I was reading. I was especially confused about the '$object->method($arguments);' because they werent in the book at all. Thanks for the descriptions.

I actually got this to do what I wanted to do. I can probably clean up the echo statments but I dont really know how. The way DW set it up I have to call on the variable by using this

$rssetup->Fields('mobo')

as opposed to what I tried at first.

$mobo

----------------------------
Would you mind filling me in on how I could clean up these echo statments so I dont need as many in each block.
_______________________________________________________


<?php
echo &quot;<table width='50%' border='2' cellspacing='0' cellpadding='0'>&quot;;
if ( $rssetup->Fields('mobo') != &quot;&quot; )
{
echo &quot;<tr><td>&quot;;
echo $rssetup->Fields('mobo');
echo &quot;</td></tr>&quot;;
}
if ( $rssetup->Fields('ram') != &quot;&quot; )
{
echo &quot;<tr><td>&quot;;
echo $rssetup->Fields('ram');
echo &quot;</td></tr>&quot;;
}
if ( $rssetup->Fields('video_card') != &quot;&quot; )
{
echo &quot;<tr><td>&quot;;
echo $rssetup->Fields('video_card');
echo &quot;</td></tr>&quot;;
}
?>
 
Code:
<?php
    echo &quot;<table width='50%' border='2' cellspacing='0' cellpadding='0'>&quot;;
    if ( $rssetup->Fields('mobo') != &quot;&quot; )
{      
    echo &quot;<tr><td>&quot;; 
    echo $rssetup->Fields('mobo'); 
    echo &quot;</td></tr>&quot;;
}

can be easily compacted by concatenating:
Code:
<?php
    echo &quot;<table width='50%' border='2' cellspacing='0' cellpadding='0'>&quot;;
    if ( $rssetup->Fields('mobo') != &quot;&quot; )
{      
    echo &quot;<tr><td>&quot;.$rssetup->Fields('mobo').&quot;</td></tr>&quot;;
}

Even more compacted code could make it a one-liner using the ternary operator condition?val-when-true:val-when-false;
Code:
    echo($rssetup->Fields('mobo')!= &quot;&quot;?&quot;<tr><td>&quot;.$rssetup->Fields('mobo').&quot;</td></tr>&quot;:'';

Yes, DW refers to the retrieved values through the object, as it retrieves them in the instance of the class.
 
Thanks so much. Most people in the PHP forums only want to debate when I mention Dreamweaver and PHP ;). So the period is what I need to &quot;concate&quot; the line. I'm going to use your first example since its more recognizable as a conditional statement as opposed to the terminal statement.


I'm going to mess around with the extract statment to see if I will be able to call on the variables by only using $variablename as opposed to the retrieving the values through the object. Thanks again for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top