...

View Full Version : foreach() printing out way to many lines...



DiarYofaMadmaN
05-05-2004, 03:27 PM
$link = mysql_connect ($Host, $User, $Password);
mysql_select_db($DBName, $link);

$Query = "Select Manufacturers.Manufacturer
From Manufacturers, Products
Where Manufacturers.Man_ID=Products.Man_ID
";

$Result = mysql_query ($Query);
if ($Result) {
while ($Row = mysql_fetch_array($Result)) {
foreach($Row as $e) {
echo $e."<br>";
}
}
} else {
print ("The query could not be executed!<BR>\n");
}
mysql_close ($Link);

I think the reason why it's printing out to many lines i think about 55 lines is because of the foreach function.. If i substitute the foreach for just echo $Row; it prints out Array 10 times on the page which is the right amount of lines(I counted it out in mysql database). I was reading about foreach in the php manual and doesn't state anything about that problem.... Can anyone help me out?

to view what I'm talking about go here http://www.maxforcepc.com/edit/test.php

There should only be 10 Soyo and 9 Western Digital words being printed out in that order.

-Peace

o0katz0o
05-05-2004, 03:37 PM
how many columns per row?
it should work I think, unless you have one row
try echo current($Row);

firepages
05-05-2004, 05:09 PM
use
mysql_fetch_assoc()
or
mysql_fetch_array($Result,MYSQL_ASSOC);

when you fetch mysql_array() you get data in an associative array AND a numeric indicexed array. , e.g. if field[0] in your DB is called 'Joe' with the value 'bloggs' mysql_fetch_array returns both $row[0] && $row['Joe'] , both equalling 'bloggs'

DiarYofaMadmaN
05-05-2004, 11:04 PM
these are my column names:



+----+----------+--------------+----------------+---------+--------------+-------------+------+------------+-----------+---------+----------------------+----------+---------+--------------+--------------+
| ID | Category | Sub_Category | Product_Number | Product | Manufacturer | Description | Cost | Sell_Price | Tax_Price | Taxable | Sell_Internationally | In_Stock | Quanity | Show_Quanity | Dumby Field |
+----+----------+--------------+----------------+---------+--------------+-------------+------+------------+-----------+---------+----------------------+----------+---------+--------------+--------------+

raf
05-05-2004, 11:21 PM
Euh ... Not sure what you are trying to do here. Do you want to print the manafacturers name ?


while ($row = mysql_fetch_assoc($result)) {
echo $row['Manufacturer'] . '<br />' ;
}

DiarYofaMadmaN
05-06-2004, 12:21 AM
Sorry for not explaining myself


$Query = "Select Manufacturers.Manufacturer
From Manufacturers, Products
Where Manufacturers.Man_ID=Products.Man_ID
";
Joins the Manufacturer table in Manufacturer to the number filed in Column Man_ID in the table Products.. in the Manufacturer's table there is Man_ID which is set to auto number which is the number of the Manufacturer name in the right column. The Query statement joins them so it replaces the numbers under the Manufacturer Column in the Products table.

I'm haveing a hard time printing out just that.. The problem I'm having is it keeps on printing more names then what it should be printing.

-peace I'll fool around with the previous post codes Thanks for the help guys. If anyone else would like to help that would be greatly appreciated :-)

-peace

DiarYofaMadmaN
05-06-2004, 12:34 AM
use
mysql_fetch_assoc()
or
mysql_fetch_array($Result,MYSQL_ASSOC);

when you fetch mysql_array() you get data in an associative array AND a numeric indicexed array. , e.g. if field[0] in your DB is called 'Joe' with the value 'bloggs' mysql_fetch_array returns both $row[0] && $row['Joe'] , both equalling 'bloggs'

Thanks, i read up more about mysql_assoc and understand it better



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum