Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    foreach() printing out way to many lines...

    PHP Code:
    $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

  • #2
    New Coder
    Join Date
    Apr 2004
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how many columns per row?
    it should work I think, unless you have one row
    try echo current($Row);

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    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'
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    these are my column names:

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

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Euh ... Not sure what you are trying to do here. Do you want to print the manafacturers name ?
    PHP Code:
    while ($row mysql_fetch_assoc($result)) { 
         echo 
    $row['Manufacturer'] . '<br />' ;

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #6
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for not explaining myself

    PHP Code:
    $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
    Last edited by DiarYofaMadmaN; 05-05-2004 at 11:24 PM.

  • #7
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •