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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.