SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replacing Empty Database Cell with Variable

    Suppose you have a column in a database table that includes some empty cells. Is there a way to devise your query or dynamic table display so that each empty cell displays a certain value?

    In particular, I have a table with nicknames and quotes for all the world's nations and states. But cells representing jurisdictions for which I haven't yet tracked down a nickname or quote will feature something like this instead:

    <a href="<?php echo "$suggest" ?>">(“Suggest a Quote”)</a>

    But it would be so much simpler if I could leave these cells empty, then insert this code with my query or dynamic table code. So Alaska would display its nickname ("Land of the Midnight Sun"), while Kenya's blank cell would be replaced by <a href="<?php echo "$suggest" ?>">(“Suggest a Quote”)</a>

    I just don't know how to do it. Any tips?

    Thanks.

  2. #2
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lemme know if i'm missing something, but it seems like you can fix this with an if statement:
    Code:
    if(!isset($quote) || trim($quote) == '')
    { echo("<a href=\"suggestpage.php?id=$rowid\">(Suggest a Quote)</a>"); }

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Darchangel
    lemme know if i'm missing something, but it seems like you can fix this with an if statement:
    Code:
    if(!isset($quote) || trim($quote) == '')
    { echo("<a href=\"suggestpage.php?id=$rowid\">(Suggest a Quote)</a>"); }
    Thanks. Now let me see if I can figure out how to plug that in.

    Here's the code I use for my nicknames/subtitles and quotes:

    PHP Code:
      <div class="subtitle" id="sub<?php echo "$mycode?>"><span class="subspan">
      <?php foreach ($data as $r) { print $r['Nickname']; } ?></span></div>
      <div class="quotewrap" id="qwrap<?php echo "$mycode?>">
        <div class="quote" id="q<?php echo "$mycode?>">
    <?php foreach ($data as $r) { print $r['Quote']; } ?>
        </div>
        <div class="sig" id="sig<?php echo "$mycode?>">
    <?php foreach ($data as $r) { print $r['Signature']; } ?>
        </div>
      </div>
    Using your fix, would I change it to something like this?:

    PHP Code:
      <div class="subtitle" id="sub<?php echo "$mycode?>"><span class="subspan">

    if(!isset($nickname) || trim($nickname) == '')
    { echo("<a href=\"suggestpage.php?id=$rowid\">(Suggest a Nickname)</a>"); }

    // I simply deleted this: <?php foreach ($data as $r) { print $r['Nickname']; } ?></span></div>

      <div class="quotewrap" id="qwrap<?php echo "$mycode?>">
        <div class="quote" id="q<?php echo "$mycode?>">

    if(!isset($quote) || trim($quote) == '')
    { echo("<a href=\"suggestpage.php?id=$rowid\">(Suggest a Quote)</a>"); }

    // And I deleted this: <?php foreach ($data as $r) { print $r['Quote']; } ?>

        </div>
        <div class="sig" id="sig<?php echo "$mycode?>">
    <?php foreach ($data as $r) { print $r['Signature']; } ?>
        </div>
      </div>
    Thanks!

  4. #4
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, didn't know you were putting it in the middle of html. in that case, don't forget to surround the code with <?php ?>

    also, i obviously don't know the context of your full code but your foreach sections look odd. is $data a multi-dimentional array of your entire db results?

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Darchangel
    sorry, didn't know you were putting it in the middle of html. in that case, don't forget to surround the code with <?php ?>

    also, i obviously don't know the context of your full code but your foreach sections look odd. is $data a multi-dimentional array of your entire db results?
    Sorry, I don't even know! This is all new to me. I've been trying to make a database-driven website, using tutorials and online forums, and I've received so many alternate strategies, I feel like I've created a quilt.

    This is what my primary query looks like:

    PHP Code:
    <?php
    mysql_connect 
    ("localhost" "USERNAME" "PASSWORD") or die(mysql_error());
    mysql_select_db ("DATABASE") or die(mysql_error());

    $qry "SELECT * FROM basics as b, famarea as f, bushnations as bn, bushstates as bs
     WHERE bn.IDArea = b.IDArea and bs.IDArea = b.IDArea and f.IDArea = b.IDArea and b.IDArea = '
    $mycode'";

    $result mysql_query($qry);
    $type mysql_fetch_array($result);
    $mytype "my" $type['TypeArea'];
    $
    $mytype $type['Name'];

    if (
    phpversion() <= "4.1.0") { $vars array_merge($HTTP_GET_VARS$HTTP_POST_VARS); } else { $vars $_REQUEST; }
    $area_code_sql mysql_query("SELECT *
     FROM basics as b, famarea as f, bushnations as bn, bushstates as bs
     WHERE bn.IDArea = b.IDArea and bs.IDArea = b.IDArea and f.IDArea = b.IDArea and and b.IDArea = '"
    .$mycode);

    $type mysql_fetch_assoc($result);
    $mytype "my" $type['TypeArea'];
    $data = array();
    $data_details mysql_fetch_assoc(mysql_query("SELECT *
    FROM basics WHERE IDArea LIKE '
    $mycode'"));

    /*
    $query = "SELECT * FROM bushnations WHERE IDArea like '$mycode'";
    */

    $query "SELECT famarea.*,  bushnations.* FROM famarea, bushnations
    WHERE bushnations.IDArea = famarea.IDArea
    AND  famarea.IDArea like '
    $mycode'";
    $data = array();
    if (
    $res mysql_query($query)) { while ($temp mysql_fetch_assoc($res)) { $data[] = $temp;} };
    $foo = array("us-""ca-");
    $mycode str_replace($foo""$mycode);
    ?>
    I have an array in my head section, but I can't even remember what it was intended for - or if it's even functioning. I just kept it as a reference, so I'll know how to make an array when I really need one.

    Thanks.

  6. #6
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, i still don't think i have a good idea for where the original question fits in with the most recent piece of code. i do have a few general suggestions though. i think it may be time to redo this section from scratch. there's a lot of unneccessary repetition. i'd also simplify the sql; try to only grab what you need

    you might also want to get more comfortable with arrays before you work a lot with foreach. they are a great and powerful combo - and you seem to be using foreach correctly so far - but i think you'll benefit more in the long run if you get really familiar with arrays first

    hope this was at all helpful


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
  •