Jump to content

COnfused with Loop


robert_gsfame

Recommended Posts

guys, i got confused with Looping...this is the case

 

I have 3 stocks, let say "Stock A", "Stock B", "Stock C" and the owner of each stock A --> Mr.James

stock B --> Mr.Barry

Stock C --> Mr. James

 

So A & B belong to Mr. James...and C belong to Mr Barry, now i want to display every stocks that belongs to Mr James and Mr Barry separately

 

How to do it using While?? really confused with this..thx in advance

 

 

Link to comment
Share on other sites

<?php

 

$query="SELECT * FROM table'";

$myquery=mysql_query($query);

$num=mysql_num_rows($myquery);

 

if(!empty($num)){

 

while($find=mysql_fetch_assoc($myquery)){

$stock = $find['stock'];

$owner = $find['owner'];

 

--------------PROBLEM STARTS HERE

 

}

 

I want to have this as result after ECHO

 

Record of stocks of Mr. Barry

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

Stock B

 

 

Record of stocks of Mr. James

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

Stock A

Stock C

 

 

 

Link to comment
Share on other sites

try

<?php 

$query="SELECT * FROM table ORDER BY `owner`'";
$myquery=mysql_query($query);
$num=mysql_num_rows($myquery);
$cur_name = '';
if(!empty($num)){
while($find=mysql_fetch_assoc($myquery)){
	$stock = $find['stock'];
	$owner = $find['owner'];
	if ($cur_name != $owner){
		$cur_name = $owner;
		echo "Record of stocks of $owner<br />\n";
		echo "________________________________<br />\n";
	}
	echo "Stock $stock<br />\n";
}
}
?>

Link to comment
Share on other sites

yes, blank screen! i tried my code and it works ;D

 

<?php

 

$query="SELECT DISTINCT owner FROM table'";

$myquery=mysql_query($query);

$num=mysql_num_rows($myquery);

 

if(!empty($num)){

 

while($find=mysql_fetch_assoc($myquery)){

$owner= $find['owner'];

echo $owner;

echo "<br>";

$query2="SELECT * FROM table WHERE owner='$owner'";

$myquery2=mysql_query($query2);

$num2=mysql_num_rows($myquery2);

if($num2>1){

while($find2=mysql_fetch_assoc($myquery2)){

$stock=$find2['stock'];

echo $stock;

echo "<br>";}}else

if($num2==1){

$find3=mysql_fetch_array($myquery2);

$stock2=$find3['stock'];

echo $stock2;

 

}

}

}

 

how's the code? :D

 

 

 

 

 

 

 

 

 

 

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.