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
https://forums.phpfreaks.com/topic/179157-confused-with-loop/
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
https://forums.phpfreaks.com/topic/179157-confused-with-loop/#findComment-945207
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
https://forums.phpfreaks.com/topic/179157-confused-with-loop/#findComment-945209
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
https://forums.phpfreaks.com/topic/179157-confused-with-loop/#findComment-945376
Share on other sites

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.