designingamy Posted June 17, 2008 Share Posted June 17, 2008 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 More sharing options...
KevinM1 Posted June 17, 2008 Share Posted June 17, 2008 I think some of your code is missing. Try putting it inside of CODE tags (click the # button). Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567303 Share on other sites More sharing options...
designingamy Posted June 17, 2008 Author Share Posted June 17, 2008 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> Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567315 Share on other sites More sharing options...
conker87 Posted June 17, 2008 Share Posted June 17, 2008 <?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. Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567319 Share on other sites More sharing options...
designingamy Posted June 17, 2008 Author Share Posted June 17, 2008 The $petName and $petDescription need to be taken out of the database. I'm confused on how this is done within the program. I thought the extract took the pettype out of the database, not the other 2 as well. What am I missing? ??? ~Amy Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567341 Share on other sites More sharing options...
conker87 Posted June 17, 2008 Share Posted June 17, 2008 It takes all of the values out. Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567347 Share on other sites More sharing options...
designingamy Posted June 17, 2008 Author Share Posted June 17, 2008 So, I could pull any of the information from that table in the database as long as I put $ in front of it? Now, let's say I have 2 tables to pull information from. Using the above example, how would I do that successfully? ~Amy Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567360 Share on other sites More sharing options...
conker87 Posted June 17, 2008 Share Posted June 17, 2008 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"; Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567364 Share on other sites More sharing options...
KevinM1 Posted June 17, 2008 Share Posted June 17, 2008 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). Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567388 Share on other sites More sharing options...
designingamy Posted June 17, 2008 Author Share Posted June 17, 2008 Oh, okay, I understand that now. But if I were to have to pull info from 2 tables would it look something similar to this: $query="SELECT * FROM Pet, Customers WHERE petType='$pettype'"; Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567462 Share on other sites More sharing options...
designingamy Posted June 17, 2008 Author Share Posted June 17, 2008 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? Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567476 Share on other sites More sharing options...
designingamy Posted June 17, 2008 Author Share Posted June 17, 2008 If the information is pulled from the database, how can I put it as a link that will take it to the next page that will show more information about that pet? Thanks, ~Amy Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567697 Share on other sites More sharing options...
DarkWater Posted June 18, 2008 Share Posted June 18, 2008 Just put it in an anchor tag and make a page that processes an animal ID passed to it with GET. Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567708 Share on other sites More sharing options...
designingamy Posted June 18, 2008 Author Share Posted June 18, 2008 Oh...I'm guessing like this... <td><a href="thispage.php?pet=Cat">Cat</a></td> ~Amy Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567716 Share on other sites More sharing options...
DarkWater Posted June 18, 2008 Share Posted June 18, 2008 Yeah, but dynamically as you go through the rows of animal data. Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567720 Share on other sites More sharing options...
designingamy Posted June 18, 2008 Author Share Posted June 18, 2008 Dynamically meaning?.... that the next page would have something like... $pet=$GET['pet']; $sql="SELECT * FROM table WHERE pet=$pet"; ??? Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567739 Share on other sites More sharing options...
DarkWater Posted June 18, 2008 Share Posted June 18, 2008 Inside of the for loop is where you can dynamically create the links based on the current animal name or ID or something. Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-567743 Share on other sites More sharing options...
designingamy Posted June 18, 2008 Author Share Posted June 18, 2008 Hmmmmm...I don't understand what you mean. Within the for loop? Was the code I wrote earlier not correct? Hmmmm... Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-568190 Share on other sites More sharing options...
DarkWater Posted June 18, 2008 Share Posted June 18, 2008 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> Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-568201 Share on other sites More sharing options...
designingamy Posted June 18, 2008 Author Share Posted June 18, 2008 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? Link to comment https://forums.phpfreaks.com/topic/110578-getting-data-producing-table/#findComment-568209 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.