bhavin_85 Posted February 28, 2007 Share Posted February 28, 2007 hey guys im tryin to make a table to show multiple items taken from a database it should look something like this invoice id date item name description weight price the items name, desciption, weight and price will ahve multiple rows....how do i get the page to print each row in a specific place? <tr> <td><? while($row = mysql_fetch_assoc($query)) { $cust_id = $row['cust_id']; $item_name = $row['item_name']; $description = $row['description']; $weight = $row['weight']; $price = $row['price']; $date = $row['date']; echo "$item_name $description</br>"; } ?> </td> its not possible to create a table within the <? php ?> notation is it? Quote Link to comment Share on other sites More sharing options...
papaface Posted February 28, 2007 Share Posted February 28, 2007 Yes, just do it as normal. while($row = mysql_fetch_assoc($query)) { ?> //html table here $cust_id = $row['cust_id']; $item_name = $row['item_name']; $description = $row['description']; $weight = $row['weight']; $price = $row['price']; $date = $row['date']; echo "$item_name $description</br>"; <?php } This comes out of PHP. In order to display the variables simply go back into php like <?php echo $item_name; ?> where you want to echo the variable. Quote Link to comment Share on other sites More sharing options...
offsprg01 Posted February 28, 2007 Share Posted February 28, 2007 just to help clarify <?php $someVar = date('m-d-Y'); ?> <div class="someDiv"> Today is <?php echo $someVar ?>. </div> by the way i highly reccomend you use CSS tables are fast becoming out of date i'm going to miss tables. HTH Quote Link to comment Share on other sites More sharing options...
papaface Posted February 28, 2007 Share Posted February 28, 2007 I dont think its really that tables are going out of date, its the fact that tables are only supposed to be used to present tabular data, not websites. Quote Link to comment Share on other sites More sharing options...
bhavin_85 Posted February 28, 2007 Author Share Posted February 28, 2007 hi guys ive tried to both ways and it didnt work....when i put the table into the <? ?> i get a compilation error, when i put <? echo "$item_name"; ?> into the table that already exists it only prints 1 variable, not all of the variables that are assciatied with it it should print 2 variables....any other ideas? i dont know how to write a css is it definetly worth using it over tables? Quote Link to comment Share on other sites More sharing options...
willpower Posted February 28, 2007 Share Posted February 28, 2007 OK 2 issues here. 1) Tables are tables are tables! And they SHOULD be used for presenting tabular data such as this. 2) The HTML code for creating tables <table> shoudl NOT be included in your <? ?> These marks indicate the START and STOP of your php. You can mix HTML and PHP together as the <? allows the server to say...oh i need to treat this bit differently from the HTML. <table> <tr> <td> <? echo "this is my " . $name; ?> </td> </tr> </table> Not how the <? and ?> sourround ONLY the PHP and not any HTML Quote Link to comment Share on other sites More sharing options...
papaface Posted February 28, 2007 Share Posted February 28, 2007 Also you should ALWAYS use <?php not <? for compatibility. Quote Link to comment Share on other sites More sharing options...
bhavin_85 Posted February 28, 2007 Author Share Posted February 28, 2007 hi guys this is the last poblem i promise as you can seee i have created the table and put in the variables, however because 1 variable my have more then piece of data it wont display using tables ??? it jsut comes out as a black square....any suggestions as 2 how i can do this? so that it will line up with the rows above? heres the code <? session_start(); if ( empty($_SESSION['username'])){ header("location:default.php"); exit; } $invoice_id = $_GET['invoice_id']; $date = $_GET['date']; include ('config.php'); $sql="SELECT a.cust_id, a.invoice_id, c.item_name, b.description, b.weight, b.price, a.date FROM invoices a INNER JOIN invoice_items b ON a.invoice_id = b.invoices_invoice_id INNER JOIN item c ON b.item_item_id = c.item_id WHERE a.invoice_id = '$invoice_id'"; $query=mysql_query($sql) or die(mysql_error()); ?> <html> <head> <title>Invoice</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor="#030102" text="#666666"> <table width="670" height="546" border="0" align="center" bordercolor="#000000"> <tr> <td colspan="2" align="right" height="10"> <? echo "You are logged in as " . $_SESSION['uname'] . ""?> <a href="default.php">Logout</a> </td> </tr> <tr> <td colspan="3" valign="top" width="670" height="158"> <img src="images/banner2.png" width="670" height="158" align="top"> </td> </tr> <tr> <td colspan="3" align="center" height="20"><? readfile("menu_invoice.inc"); ?> </td> </tr> <tr> <td> <table width="600" border="1" bordercolor="#FFFFFF" align="center"> <tr> <td colspan="4"><? echo "Invoice ID: $invoice_id";?> </td> </tr> <tr> <td colspan="4"><? echo "Purchase Date: $date"; ?></td> </tr> <tr> <td>Item Type</td> <td>Description</td> <td>Weight</td> <td>Price</td> </tr> <tr><td colspan="4"> <? while($row = mysql_fetch_assoc($query)) { $item_name = $row['item_name']; $description = $row['description']; $weight = $row['weight']; $price = $row['price']; echo "$item_name $description $weight $price</br>"; } ?> </td> </tr> </table> <tr> <td> </td> </tr> </table> </body> </html> [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
gazever Posted March 1, 2007 Share Posted March 1, 2007 Change empty data to " " Quote Link to comment Share on other sites More sharing options...
gazever Posted March 1, 2007 Share Posted March 1, 2007 Think i misunderstood what you meant ??? Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted March 1, 2007 Share Posted March 1, 2007 Think what you need here is : <?php <tr> <td>Item Type</td> <td>Description</td> <td>Weight</td> <td>Price</td> </tr> <tr><td> <? while($row = mysql_fetch_assoc($query)) { $item_name = $row['item_name']; $description = $row['description']; $weight = $row['weight']; $price = $row['price']; echo "<td>$item_name</td><td>$description</td><td>$weight</td><td>$price</td>"; } ?> </td> </tr> ?> Then what is meant by Change empty data to " " is where you have a <td></td> fill it with <td> </td> Hope that helps. Good Luck Quote Link to comment Share on other sites More sharing options...
bhavin_85 Posted March 1, 2007 Author Share Posted March 1, 2007 cheers guys had to change that code slightly but got it 2 work eventually using the following code <? while($row = mysql_fetch_assoc($query)) { $item_name = $row['item_name']; $description = $row['description']; $weight = $row['weight']; $price = $row['price']; echo "<tr><td>$item_name</td><td>$description</td><td>$weight</td><td>$price</td></tr></br>"; } ?> I had to create a new row in that echo so it gave each new item its own row cheers 4 the helpp Bhav Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.