Jump to content

Recommended Posts

I'm new and I have some questions on something I am working on.  I am using the PHP & MySQL for Dummies book, and I must be a dummie because I don't understand something they've written and I need to use.  A person enters in a type of pet and the php tells it to pull info from the database into a table.  Here's what they've written:

 

<html>

<head><title>Pet Catalog</title></head>

<body>

<?php

  $user="catalog";

  $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.");

  $nrows=mysqli_num_rows($result);

 

/*Displays results in a table */

echo "<h1>Horses</h1>;

echo "<table cellspacing='15'>";

echo "<tr><td colspan='4'><hr /></td></tr>";

for ($i=0;$i<$nrows;$i++)

{

      $n=$i + 1;

      $row = mysqli_fetch_assoc($result);

      extract ($row);

      $f_price=number_format($price,2);

        echo "<tr>\n

                <td>$n.</td>\n

                <td>$petName</td>\n

                <td>$petDescription</td>\n

                <td style='text-align: right'>\$$f_price</td>\n

                </tr>\n";

        echo "<tr><td colspan='4'><hr></td></tr>\n"

  }

echo "</table>\n";

?>

</body></html>

 

 

 

 

 

OKay, I write all of that because I don't understand a few things in there.

1) Near the bottom, what is the "\n"?

2) Near the bottom, I'm confused where the $n, $petName, $petDescription are coming from.  Where is that information being pulled from?  The database? And if so, aren't those functions and shouldn't they be included at the beginning of the program?

 

Thanks bunches in advance to anyone who can help!

~Amy

 

Link to comment
https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/
Share on other sites

Is this any better?

 

 

/* Program:     petDescripFor.php
* Description: Displays a numbered list of all pets in selected category.
*/
?>
<html>
<head><title>Pet Catalog</title></head>
<body>
<?php
  $user="catalog";
  $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.");
  $nrows=mysqli_num_rows($result);

echo "<h1>Horses</h1>
echo "<table cellspacing='15'>";
echo "<tr><td colspan='4'><hr /></td></tr>";
for ($i=0;$i<$nrows;$i++)
{
  $n=$i + 1;
  $row=mwsqli_fetch_assoc($result);
  extract($row);
  $f_price=number_format($price,2);
  echo"<tr>\n
         <td>$n.</td>\n
         <td>$petName</td>\n
         <td>$petDescription</td>\n
         <td style='text-align: right'>\$$f_price</td>\n
  echo "<tr><td colspan='4'><hr></td></tr>\n";
}
echo "</table>\n";
?>
</body></html>

 

 

 

<?php /* Program:     petDescripFor.php
* Description: Displays a numbered list of all pets in selected category.
*/
?>
<html>
<head><title>Pet Catalog</title></head>
<body>
<?php
  $user="catalog";
  $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.");
  $nrows=mysqli_num_rows($result);

echo "<h1>Horses</h1>";
echo "<table cellspacing='15'>";
echo "<tr><td colspan='4'><hr /></td></tr>";
for ($i=0;$i<$nrows;$i++)
{
  $n=$i + 1; // $n is from here.
  $row=mysqli_fetch_assoc($result);
  extract($row);
  $f_price=number_format($price,2);
  echo"<tr>\n
         <td>$n.</td>\n
         <td>$petName</td>\n
         <td>$petDescription</td>\n
         <td style='text-align: right'>$f_price</td>\n";
  echo "<tr><td colspan='4'><hr></td></tr>\n";
}
echo "</table>\n";
?>
</body></html>

1: /n is a new line. Outputs the raw html on a new line.

2: $n is $i + 1, $petName and Desc are created from extracting the array using extract();

 

Also, I fixed your code. A few things were incorrect.

Uhm. I'm not really sure. I suppose that'd be fine as long as they don't have the same field names.

 

Though, to be honest, I wouldn't use extract anyway. Use the already stated mysql_fetch_assoc.

 

for ($i=0;$i<$nrows;$i++)
{
  $n=$i + 1; // $n is from here.
  $row=mysqli_fetch_assoc($result);
  extract($row);
  $f_price=number_format($price,2);
  echo"<tr>\n
         <td>$n.</td>\n
         <td>".$row['petName']."</td>\n
         <td>".$row['petDescription']."</td>\n
         <td style='text-align: right'>$f_price</td>\n";
  echo "<tr><td colspan='4'><hr></td></tr>\n";
}
echo "</table>\n";

The key to your confusion lies with the SQL query.  Since you're selecting all values (SELECT *....), you're extracting all values.  If you limited your query (SELECT petName....), then you'd only extract those values you explicitly specify.  The variables appear to 'magically' work because of two things:

 

mysqli_fetch_assoc()

 

and

 

extract().

 

The first tells it to use the DB column names in order to reference the results, and the second pulls the results and assigns them to variables constructed out of those column names (http://us3.php.net/extract).

Also, one more question that goes along with that.  Say I want the pet information to come up in the table.  But I would like to have the pet description to come up as a link to be able to go to another page that shows more information about the animal.  Any ideas on how this could be done?

No, no, it was.  But say you wanted to create links for each and every database entry to allow users to view more info.  You could so something like this in the for loop.

 

printf('<a href="animals.php?animal=%1$s">%1$s</a>',$petName);

 

Which will output (for example):

 

<a href="animals.php?animal=Cat">Cat</a>

Okay, I am going to pull just a snipit of the last example and tell me if what I throw in would work or not.  (look towards the bottom for the a href)

 

echo "<h1>Horses</h1>";
echo "<table cellspacing='15'>";
echo "<tr><td colspan='4'><hr /></td></tr>";
for ($i=0;$i<$nrows;$i++)
{
  $n=$i + 1; // $n is from here.
  $row=mysqli_fetch_assoc($result);
  extract($row);
  $f_price=number_format($price,2);
  echo"<tr>\n
         <td>$n.</td>\n
         <td><a href="nextpage.php?pet=$petname">$petName</td>\n
         <td>$petDescription</td>\n
         <td style='text-align: right'>$f_price</td>\n";
  echo "<tr><td colspan='4'><hr></td></tr>\n";
}
echo "</table>\n";


 

Would that be enough so if they clicked that pettype it would go to the next page and pull the info for that pettype?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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