dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: Defining variables

  1. #1
    Join Date
    Jun 2011
    Posts
    9

    Defining variables

    I am trying to learn php and am having trouble with the following code. I get notices for undefined variables price, petname, and petdescription when I run the code and no output. Any ideas what my problems are. all help will be appreciated
    <?php
    /* Program: petDisplay.php
    * Desc: Displays all pets in selected category.
    */
    ?>
    <html>
    <head><title>Pet Catalog</title></head>
    <body>
    <?php
    $user="user";
    $host="localhost";
    $password="";
    $database = "PetCatalog";
    $cxn = mysqli_connect($host,$user,$password,$database)
    or die ("couldn't connect to server");
    $pettype = "horse"; //horse was typed in a form by user
    $query = "SELECT * FROM Pet WHERE petType='$pettype'";
    $result = mysqli_query($cxn,$query)
    or die ("Couldn't execute query.");

    /* Display results in a table */
    $pettype = ucfirst($pettype)."s";
    echo "<h1>$pettype</h1>\n";
    echo "<table cellspacing='15'>\n";
    echo "<tr><td colspan='3'><hr /></td></tr>\n";
    while($row = mysqli_fetch_assoc($result))
    {
    extract($row);
    $f_price = number_format($price,2);
    echo "<tr>\n
    <td>$petName</td>\n
    <td>$petDescription</td>\n
    <td style='text-align: right'>\$$f_price</td>\n
    </tr>\n";
    echo "<tr><td colspan='3'><hr /></td></tr>\n";
    }
    echo "</table>\n";
    ?>
    </body></html>

  2. #2
    Join Date
    Aug 2011
    Location
    Worthing, West Sussex
    Posts
    96
    Messy messy messy!!!

    If you're learning a new code, one thing that will help you most of all is whitespace. You can never have enough whitespace. Look how much neater this is.

    HTML Code:
    <?php
    $con = mysql_connect("user", "localhost", "password", "PetCatalog");
    if(!$con) {
        die ("Could not connect: " . mysql_error());
    }
    //mysql_select_db("dbname");
    $query = mysql_query("SELECT * FROM `Pet` WHERE `petType`='$pettype';") or die ("Error: " . mysql_error());
    $pettype = ucfirst($_POST['type']) . "s";
    ?><h1>$pettype</h1>
    <table cellspacing="15">
        <tr>
            <td colspan="3"><hr /></td>
        </tr>
    	<?php
    	while($fetch = mysql_fetch_array($query)) {
    		list($price, $petName, $petDescription) = $fetch; // The variables here are straight from your database in order.  The variables can be what you wish.
    		$f_price = number_format($price,2);
    	?><tr>
    		<td><?=$petName?></td>
    		<td><?=$petDescription?></td>
    		<td style="text-align:right;">$<?=$f_price?></td>
    	</tr>
    	<tr>
        	<td colspan="3"><hr /></td>
    	</tr>
    	<?php }?>
    </table>
    Last edited by dangerousprince; 10-04-2011 at 04:33 PM. Reason: Error in script

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles