Results 1 to 5 of 5

Thread: Some PHP help!

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Some PHP help!

    Hopefully this is a simple problem:
    I have a while statement in my php code that prints out the rows from a mysql database. This works fine.

    PHP Code:
    while($info mysql_fetch_array$result ))
          {
          print 
    "<div>" $info['tablerow'] . "</div>";
          } 
    My question is: can I use an IF statement within the while statement that doesn't effect all of the results. e.g.

    PHP Code:
    while($info mysql_fetch_array$result ))
          {
          if (
    $info['anothertablerow'] == true)
               {
               print 
    "This text is printed only for rows with a value";
               }
          else
               {
               print 
    " ";
               }
          print 
    "<div>" $info['tablerow'] . "</div>";
          } 

  2. #2
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    Ok, to elaborate on what i'm trying to acheive:

    I am creating a list of shops for a 'find a dealer' page

    I have a list of UK counties in a MySQL database and from that I produce a form like this:

    <select name="county">

    <option value="all" <?php if ($submitted == "all") { print "selected=\"selected\""; } else { print ""; } ?> >Please Select...</option>

    <?php while ($countyinfo = mysql_fetch_array($countyresult)) { ?>
    <option value="<?php
    print $countyinfo['county']; "><?php print $countyinfo['county']; ?></option>
    <?php } ?>

    </select>

    The reason I want to put an IF Statement in is because - if the form is submitted I want that value to remain selected.

    e.g.
    (to be placed within the while)

    if ($selected = $countyinfo['county'])
    {
    print "selected=\"selected\"";
    }
    else
    {
    print "";
    }

    Hope this makes sense...

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes. You could have just tried it and seen for yourself
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    Got it to work now, it was because i was putting select="selected" in with php AND html, should have looked abit closer, my bad.
    Last edited by jc_gmk; 08-01-2007 at 07:58 AM.

  5. #5
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    If I were you, I'd use some single quotes...prevents all the backslashes, and single quotes are parsed faster. It's all preference, though, I guess.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

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
  •