SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast adamschroeder's Avatar
    Join Date
    Mar 2011
    Location
    World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    having trouble with while

    Hi guys,
    With the first $get query, I'm trying to present the user with all members from a certain age group. But inside that query I want to add all countries that specific member wants to see by using the $getCountry query.
    The problem is the information echoeing out under "all countries I want to see".
    Let's say I have Adam wanting to see USA and Ecuador. Ben wants Mexico and Tom England. The information will come out as:

    1. Name:Adam
    SOA: Urgent
    all countries I want to see: USA, Ecuador

    2. Name:Ben
    SOA: Urgent
    all countries I want to see: USA, Ecuador, Mexico

    3. Name:Tom
    SOA: Urgent
    all countries I want to see: USA, Ecuador, Mexico, England.

    Why is it doing that. I tried playing around with the while loop but nothing. All I want it to do is present each member with his countries.

    PHP Code:
    $get mysql_query("SELECT * FROM mainweb WHERE age='$age_form' AND who='civilian'");

    while (
    $row mysql_fetch_assoc($get))
    {

    $username$row['username'];

    $soa$row['soa'];

    echo
    "<p><span style='color: rgb(51, 102, 255);'>SOA</span>: $soa<p />";

    echo
    "<p><span style='color: rgb(51, 102, 255);'>all countries I want to see</span>:";
    $getCountry mysql_query("SELECT country FROM mainweb WHERE username='$username' AND who='civilian'");
    while (
    $row mysql_fetch_array($getCountry)) {
        
    $cwb_countires[] = $row['country'];
    }
         foreach (
    $cwb_countires as $country)
          echo 
    $country,";

    <
    hr size='1'><p>";


  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You don't initialize the countries array before the second while loop, so it'll just add the countries to the ones already present (= the ones from the previous members).

  3. #3
    SitePoint Enthusiast adamschroeder's Avatar
    Join Date
    Mar 2011
    Location
    World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But I can't I thought.
    I initializing the countries array from within the $row = mysql_fetch_array. How can I initialize the array without fetching it as an array first?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    $get = mysql_query("
      SELECT * 
      FROM mainweb 
      WHERE age='$age_form' 
      AND who='civilian'
    ");
    while ($row = mysql_fetch_assoc($get)) {
      $username= $row['username'];
      $soa= $row['soa'];
      echo"<p><span style='color: rgb(51, 102, 255);'>SOA</span>: $soa<p />";
      echo"<p><span style='color: rgb(51, 102, 255);'>all countries I want to see</span>:";
      $getCountry = mysql_query("
        SELECT country 
        FROM mainweb 
        WHERE username='$username' 
        AND who='civilian'
      ");
    
      // initialize the array before entering the while loop
      $cwb_countires= array();  
    
      while ($row = mysql_fetch_array($getCountry)) {
        $cwb_countires[] = $row['country'];
      }     
      foreach ($cwb_countires as $country) echo " $country,";
      <hr size='1'><p>";
    }

  5. #5
    SitePoint Enthusiast adamschroeder's Avatar
    Join Date
    Mar 2011
    Location
    World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked Guido thanks.
    But why is that if I don't initialize the countries array before the second while loop, it just adds the countries to the ones already present?
    I mean, theoretically why do I have to declare $cwb_countires as an array before the loop starts?

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Because if you don't tell the parser to empty the array for each member, it just adds new elements to the array.


Bookmarks

Posting Permissions

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