Results 1 to 2 of 2

Thread: Defining variables

  1. #1
    Join Date
    Jun 2011

    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
    /* Program: petDisplay.php
    * Desc: Displays all pets in selected category.
    <head><title>Pet Catalog</title></head>
    $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))
    $f_price = number_format($price,2);
    echo "<tr>\n
    <td style='text-align: right'>\$$f_price</td>\n
    echo "<tr><td colspan='3'><hr /></td></tr>\n";
    echo "</table>\n";

  2. #2
    Join Date
    Aug 2011
    Worthing, West Sussex
    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:
    $con = mysql_connect("user", "localhost", "password", "PetCatalog");
    if(!$con) {
        die ("Could not connect: " . mysql_error());
    $query = mysql_query("SELECT * FROM `Pet` WHERE `petType`='$pettype';") or die ("Error: " . mysql_error());
    $pettype = ucfirst($_POST['type']) . "s";
    <table cellspacing="15">
            <td colspan="3"><hr /></td>
    	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);
    		<td style="text-align:right;">$<?=$f_price?></td>
        	<td colspan="3"><hr /></td>
    	<?php }?>
    Last edited by dangerousprince; 10-04-2011 at 03:33 PM. Reason: Error in script
    Titanium Web Designs Ltd - Bespoke websites and online systems

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