Jump to content

[SOLVED] use echo or printf?


bhavin_85

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/40564-solved-use-echo-or-printf/
Share on other sites

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.

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?

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

 

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]

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

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

Archived

This topic is now archived and is closed to further replies.

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