Hi,
Anyone please help out. By using select query i am getting this following output.

itemname idno name price
A B01 sa1 10
B B01 sa1 40
C B01 sa1 50
D B01 sa1 100

But i don't want this(as above one) type of display in my browser. I need like this output in my browser:

idno name itemname A itemname B itemname C itemname D
B01 sa1 10 40 50 100

How to write in php script?thanks in advance

Hi,
Anyone please help out. By using select query i am getting this following output.

itemname idno name price
A B01 sa1 10
B B01 sa1 40
C B01 sa1 50
D B01 sa1 100

But i don't want this(as above one) type of display in my browser. I need like this output in my browser:

idno name itemname A itemname B itemname C itemname D
B01 sa1 10 40 50 100

How to write in php script?thanks in advance

I just had to do something similar for a shopping cart I designed, so it may give you an idea of how to do it for your application:

Query that drives the whole thing:

mysql_select_db($database_images, $images);
$query_cart_type = "SELECT image_table.id, image_table.image, image_table.original_name, image_table.`sequence`, image_table.target, image_table.product_id, image_table.short_description, image_table.long_description, image_table.price, image_table.start_date, image_table.end_date, image_table.comments, image_target.target_id, image_target.target_page FROM image_table, image_target WHERE image_table.target=image_target.target_id AND image_table.start_date < $current_time and image_table.end_date > $current_time AND image_table.target = $catergory_get ORDER BY image_table.sequence ASC";
$cart_type = mysql_query($query_cart_type, $images) or die(mysql_error());
$row_cart_type = mysql_fetch_assoc($cart_type);
$totalRows_cart_type = mysql_num_rows($cart_type);

Then I had to put it into an array that I could walk through as I pleased, so I did this (leaving some of my troubleshooting commented out sections in it:

$rowcount = 1;
if($totalRows_cart_type >0){
do {
            //print "Row $rowcount<br />";
    		foreach($row_cart_type as $key => $val){
				$CartContent[$rowcount][$key] = $val;
				//print "<B>$key</B>: $val<br />";	
			}
            //print "<br />";
            ++$rowcount;
        } while ($row_cart_type = mysql_fetch_assoc($cart_type));
		//var_dump($CartContent);
	$cart_total = $rowcount -1;
}

Then the display is a repeating <td></td> pair with the </tr><tr> added when I've displayed the number of columns that I want to display - in this case it is 4 columns.

<?php if ($totalRows_cart_type > 0) { // Show if recordset not empty ?>
  <table width="700" align="center" cellspacing="4">
    <tr><td colspan="3" align="center" valign="middle"><h1><strong>
      Shop our <?php echo $CartContent[1]['target_page']; ?>
      </strong></h1></td></tr>
      
    <?php $pic_counter = 1;?>
    <?php while($pic_counter <= $cart_total){?>
    <tr align="center" valign="top">
      <?php for($j = 1; $j <= 4; $j++){ ?>
      <td width="175" bgcolor="#EEEEEE">
        <?php if($pic_counter <= $cart_total){?>
        <a href="<?php echo $CartContent[$pic_counter]['image']?>" target="_blank"><img src="<?php echo $CartContent[$pic_counter]['image']?>" width="100" alt="<?php echo  $CartContent[$pic_counter]['short_description'];?>" /></a>
        <br /><?php echo  $CartContent[$pic_counter]['short_description'];?>
        <br />$<?php echo  $CartContent[$pic_counter]['price'];?>
        <br /><a href="prod_library.php?image_id=<?php echo  $CartContent[$pic_counter]['id'] ?>"><img src="images/detail_btn.png" alt="more details" width="100" border="0" /></a>
        <<br />
        <?php }?>
        </td>
      <?php $pic_counter++; }?>
      </tr>
    <tr><td height="1" colspan="4"></td></tr>
    <?php }?>
  </table>

Hope this helps, and if someone has a better cleaner way to do it; I'd be happy to see it, since I'm still a neophyte in the PHP world.

thanks but i can't understand.Give me some other solution and also see the questions what i posted.

thanks but i can't understand.Give me some other solution and also see the questions what i posted.

Let me try it this way.

1) Take the output of your query that has all the data you want to display.

2) set up a loop that will read all the records keeping the key from your query so you can easily access the array' field you want to display.

3) set up a simple table with the <tr></tr> table row pairs, and put inside them the <td></td> pair that will display the data in column format like you want.

4) then set up acounter for a loop for the columns, and then a counter for the row of data uou want.

By using the table you control the spacing and formatting you want.

The code I put up there can be seen in the shopping cart at the website in my profile.

Hope this helped.

thanks but i can't understand.Give me some other solution and also see the questions what i posted.

Here's the interpretation of what I said above. It gives you the result you want.

mysql_select_db($database_example, $example);
$query_Recordset1 = "SELECT * FROM example";
$Recordset1 = mysql_query($query_Recordset1, $example) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$rowcount = 1;
do{
	foreach($row_Recordset1 as $key => $value){
		$CartContent[$rowcount][$key] = $value;
	}
	++$rowcount;
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="400" cellspacing="2" cellpadding="3">
  <tr>
    <td>idno</td>
    <td>itemname</td>
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>D</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
<?php // a loop would typically go here, but since you have only one set
$i = 1;
$a = 1;
$b = 2;
$c = 3;
$d = 4;
?>
  <tr>
    <td><?php echo $CartContent[$i]['idno']; ?></td>
    <td><?php echo $CartContent[$i]['name']; ?></td>
    <td><?php echo $CartContent[$a]['price']; ?></td>
    <td><?php echo $CartContent[$b]['price']; ?></td>
    <td><?php echo $CartContent[$c]['price']; ?></td>
    <td><?php echo $CartContent[$d]['price']; ?></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>

</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

The table SQL that I used to drive this example is:

-- phpMyAdmin SQL Dump
-- version 3.3.7
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jul 06, 2011 at 01:38 PM
-- Server version: 5.1.51
-- PHP Version: 5.2.14

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `test`
--

-- --------------------------------------------------------

--
-- Table structure for table `example`
--

CREATE TABLE IF NOT EXISTS `example` (
  `item_id` int(11) NOT NULL AUTO_INCREMENT,
  `itemname` varchar(45) NOT NULL,
  `idno` varchar(45) NOT NULL,
  `name` varchar(45) NOT NULL,
  `price` decimal(8,2) NOT NULL,
  PRIMARY KEY (`item_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `example`
--

INSERT INTO `example` (`item_id`, `itemname`, `idno`, `name`, `price`) VALUES
(1, 'A', 'B01', 'sa1', 10.00),
(2, 'B', 'B01', 'SA1', 40.00),
(3, 'C', 'B01', 'sa1', 50.00),
(4, 'D', 'B01', 'SA2', 100.00);

I think I've given you a complete answer now. The attachment has the output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.