?
Solved

Only fetch first row

Posted on 2009-04-23
6
Medium Priority
?
545 Views
Last Modified: 2013-12-12
Hi!

This is my first time ever developing web application in PHP. I got a problem in getting the the list of data by item_description in item table. The output only display for the first item_description. (see attached image)

Sorry if the point too low. I'm running out of points.
<?php
session_start();
 
include "conn.php";
 
$out_cat2="";
$sql88="SELECT * FROM item_description";
$result88=@mysql_query($sql88,$conn) or die(mysql_error());
while($row88=@mysql_fetch_array($result88))
{
$out_cat2.=$row88['item_description'];
?>
<?php echo $row88['item_description']; ?><br />
<?php 
 
$cbNo=0;
$first="";
$out_OS="";
$sql1="SELECT * FROM item WHERE item_description='$out_cat2'";
$result1=@mysql_query($sql1,$conn) or die(mysql_error());
while($row1=@mysql_fetch_array($result1))
{	if($row1['item_status']=="available")
	{ 	$second=$row1['item_nama'];
		if($second!=$first)
		{ $cbNo++;
		  $out_OS.="<input type=checkbox name=cb".$cbNo." value=".$row1['item_id'].">&nbsp;".$row1['item_nama']."<br>";
		}
		$first=$row1['item_nama'];
	}
}
 
?>
<?php echo $out_OS; ?><br />
<?php
}
?>

Open in new window

output.jpg
0
Comment
Question by:fird
  • 3
  • 3
6 Comments
 
LVL 14

Expert Comment

by:shobinsun
ID: 24213220
HI,

For first row only:

use:

$sql88="SELECT * FROM item_description LIMIT 1";
0
 

Author Comment

by:fird
ID: 24213255
Hi shobinsun,

Thanks for replying.

Maybe my question is not clear enough. Actually what I want is each item_description (Multimedia, Operating System, Bubble Jet, Laptop, Utilities) will display the item_name under resepective item_description. Currently my output that I got is, only Multimedia displaying the item_name.
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24213401
Hi,

could you please send me the database tables too.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:fird
ID: 24216044
Hi,

Database table as per attachment
item.pdf
item-description.pdf
0
 
LVL 14

Accepted Solution

by:
shobinsun earned 200 total points
ID: 24222335
Hi,

Use the following code:

only one mistake in the code:

$out_cat2.=$row88['item_description'];  should be:   $out_cat2 = $row88['item_description'];


Regards
<?php
session_start();
 
mysql_connect("localhost", "root","") or
    die ("Could not connect to database");
mysql_select_db("pdftest") or
    die ("Could not select database");
 
 
$out_cat2="";
$sql88="SELECT * FROM item_description";
$result88=mysql_query($sql88) or die(mysql_error());
while($row88=mysql_fetch_array($result88))
{
$out_cat2=$row88['item_description'];
?>
<?php echo $row88['item_description']; ?><br />
<?php 
 
$cbNo=0;
$first="";
$out_OS="";
$sql1="SELECT * FROM item WHERE item_description='$out_cat2'";
$result1=mysql_query($sql1) or die(mysql_error());
//echo "<br>Num of Rows:", mysql_num_rows($result1);
while($row1=mysql_fetch_array($result1))
{       if($row1['item_status']=="available")
        {       $second=$row1['item_nama'];
                if($second!=$first)
                { $cbNo++;
                  $out_OS.="<input type=checkbox name=cb".$cbNo." value=".$row1['item_id']."> ".$row1['item_nama']."<br>";
                }
                $first=$row1['item_nama'];
        }
}
 
?>
<?php echo $out_OS; ?><br />
<?php
}
?>

Open in new window

0
 

Author Comment

by:fird
ID: 24270352
Thx shobisun, it's worked! Sorry for a late reply my internet connection got problem.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question