SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need some help breaking and specifying values in a array

    PHP Code:
    $sql2 'SELECT id,shoename FROM regularmenu WHERE menusubject_id = '.mysql_real_escape_string($cat).';'
    $result2 mysql_query($sql2); 
    $item = array();
    if(
    $result2 && mysql_num_rows($result2)!=0) { 

    echo 
    "\n\t\t",'<ul class="submenu">',"\n"

    while(
    $row2 mysql_fetch_assoc($result2)) { 
    $item [] = $row2

    some code...........

    Now I take the variable "$item" which contain the array o value of the query above. as you can see the script below will take the values of the variable item and will loop the value only when it matches the id number 9 in other words it will only loop if id is equal 9 and then it will break out of the loop and stop.

    PHP Code:
    $item;
    while(
    $row mysql_fetch_assoc($result)) {
    if(
    $row['id']==9) {
    $item $row;
    break;
    }

    I want to break the loop if it doesn't match more than one id no just one. I don't know how to script it other than specifying it directly in the query like this:
    SELECT * FROM table WHERE id IN (9,10,15,24,35,46)
    I want to avoid repeatinting a query when there is a query with the same table in the script. I have been researching and reading but I haven't find a way on how to script the id's directily in the loop condition instead of creating another query when already exist in the script.

    any suggestions?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This will break as soon as the record id is either 1,8,4,5,7 or 3.

    PHP Code:
    <?php
    $rResult 
    mysql_query("SELECT id, name, email, town FROM users WHERE town = 'sitepoint'");
    while(
    $aRecord mysql_fetch_assoc($rResult))
    {
        if(
    true === in_array($aRecord['id'], array(1,8,4,5,7,3)))
        {
            break;
        }
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you silverbullet.

    you wrote this at the mysql_query:

    WHERE town = 'sitepoint'"
    Where does the value of sitepoint is coming?

    I know town is part of the SELECT at the query but sitepoint?

    it means that if town is not = 'sitepoint then it won't SELECT any of this values from the table ? "id, name, email, town"

    can you explain me the line below?

    if(true === in_array($aRecord['id'], array(1,8,4,5,7,3)))

    why do you compare the bool true with the in_array function? when you say indentical === to in_array is the array function being compare with the bool true as well?

    When I mean array I refer to this part you put in the script
    PHP Code:
    array(1,8,4,5,7,3))) 

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://nz2.php.net/in_array
    if(true === in_array...)
    true === is a bit over the top, in_array returns a bool
    if(in_array(1, array(1,2,3))
    is fine.

  5. #5
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so it is ok to use it without the bool true as well.

  6. #6
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want that when a users click here

    HTML Code:
    <td width="39"><a href="shoemenu.php"><img src="../images/menu.gif" alt="menu" width="100&#37;" height="22" /></a></td>
    this link take the users to this query below

    PHP Code:
    $query "SELECT shoes.shoes_name, shoes.price, shoes.moreinfo shoes.shoe_kind_id
    FROM shoes"


    $result mysql_query($query);

    while (
    $row mysql_fetch_assoc($result)) {
    if(
    in_array($aRecord['id'], array(1,8,4,5,7,3)))
        {
      echo 
    "<tr><td>" $row['shoes_name'] . "</td><td>" $row['price'] . "</td><td>" $row['moreinfo'] . "</td></tr>"
     break;

    I want that when the user click on the link <a href="shoemenu.php"></a> then it immediately echo the field shoe_name, price, and moreinfo of 1,8,4,5,7,3 rows only then break the loop.

    I am know I am missing a WHERE close in the query where is going to look similar to,

    WHERE shoe_kind_id = shoemenu.php?

    Well I don't know how to continue with the query there in order to form it property so it can echo the fields SELECTED above of the rows specified aboved.

    the above request in t his post was what I really meant at the moment I posted this thread, The only thing was that I didn't have clear the request in my mind for you guys.

    any suggestions?
    Last edited by co.ador; Jun 3, 2009 at 06:57.


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
  •