A Iittle code help with test for existing variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nathanwb
    New Member
    • Mar 2008
    • 39

    A Iittle code help with test for existing variables

    I am trying to only show the charges if they are in the database..

    I am querying a table for the subject information and also show the charges associated with each subject.

    What I am trying to do with the code is to only show charges if they exist. I know I can do it with just showing the $charge1 fields but this is how I am attempting to do it.

    Here is the page:
    [html]
    http://www.tnrcso.org/wanted.php
    [/html]here is the php code[php]<?
    include ("top.php");
    include ("nav.php");
    ?>
    <td width=75% valign=top>
    <table border=0 width=100% class=leftMenut op1>
    <td><span class='text'>
    <p class='bodyHead 1'>Roane County Sheriff's Office Most Wanted Fugitives</span></p>
    <table border=0 width=99% class="addalert ">
    <td valign=top><img src='img/alert.gif' align=left></td>
    <td><span class='text1'>T hese fugitives should be considered ARMED and DANGEROUS! NEVER attempt to apprehend a fugitive yourself.
    If you have information on the location of any of these fugitives, contact the Roane County Sheriff's Office at (865) 717-4700 or dial 911</td></table>
    <br>
    <?
    include('db.php ');
    echo "<table border=0 width=99% cellpadding=2 cellspacing=0 class='pbox14'> ";
    $query1 = mysql_query("SE LECT * FROM `wanted` ORDER BY `id`");
    while ($row1 = mysql_fetch_arr ay($query1))
    {
    $id = $row1['id'];
    $first = $row1['first'];
    $last = $row1['last'];
    $address = $row1['address'];
    $city = $row1['city'];
    $state = $row1['state'];
    $city = $row1['city'];
    $zip = $row1['zip'];
    $charge1 = $row1['charge1'];
    $charge2 = $row1['charge2'];
    $charge3 = $row1['charge3'];
    $charge4 = $row1['charge4'];
    $charge5 = $row1['charge5'];
    $charge6 = $row1['charge6'];
    $charge7 = $row1['charge7'];
    $charge8 = $row1['charge8'];
    $charge9 = $row1['charge9'];
    $charge10 = $row1['charge10'];
    $qq = mysql_num_rows( mysql_query("SE LECT * FROM `wimage` WHERE `news_id`='$id' "));
    $mug11 = ($qq>0) ? "<img src=\"../news/wimage/t$file\" border=1>" : '';

    echo "<td colspan=3 class='wanted1' >&nbsp;$first $last</td><tr>
    <td width=100 valign=top>";
    $q = mysql_query ("SELECT * from wimage WHERE news_id=$id");
    while ($Row6 = mysql_fetch_arr ay($q))
    {
    echo "<img src=\"../news/wimage/t{$Row6['file']}\" border=1></td>";
    }
    echo "</td>
    <td valign=top><spa n class='text1'>< B>$first $last</b><br>$address< br>$city, $state $zip</td>
    <td valign=top><spa n class='text1'>< b>Charges</b><bR>";
    if ($row1['charge1']==0) { echo "1. $charge1 <Br>"; } else { echo ""; };
    if ($row1['charge2']==0) { echo "2. $charge2 <Br>"; } else { echo ""; };
    if ($row1['charge3']==0) { echo "3. $charge3 <br>"; } else { echo ""; };
    if ($row1['charge4']==0) { echo "4. $charge4 <br>"; } else { echo ""; };
    if ($row1['charge5']==0) { echo "5. $charge5 <br>"; } else { echo ""; };
    if ($row1['charge6']==0) { echo "6. $charge6 <br>"; } else { echo ""; };
    if ($row1['charge7']==0) { echo "7. $charge7 <br>"; } else { echo ""; };
    if ($row1['charge8']==0) { echo "8. $charge8 <br>"; } else { echo ""; };
    if ($row1['charge9']==0) { echo "9. $charge9 <br>"; } else { echo ""; };
    if ($row1['charge10']==0) { echo "10. $charge10 <br>"; } else { echo ""; };
    echo "
    </td><tr>";
    }
    echo "</td></table><br>";
    ?>
    <table border=0 width=99% class="addalert ">
    <td valign=top><img src='img/alert.gif' align=left></td>
    <td><span class='text1'>T hese fugitives should be considered ARMED and DANGEROUS! NEVER attempt to apprehend a fugitive yourself.
    If you have information on the location of any of these fugitives, contact the Roane County Sheriff's Office at (865) 717-4700 or dial 911</td></table>
    <br><br><Br>
    </td></table>
    </td></table>
    <?
    include ("bot.php");[/php]Thank you
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    I really do not understand the question. Does [php]if ($row1['charge1']==0)[/php]mean that the charge exists when it is zero (0)? If not, please explain a bit more.

    Ronald

    Comment

    • aktar
      New Member
      • Jul 2006
      • 105

      #3
      Hi Nathan,

      put this in place where you have all those charges:

      [CODE=php]
      $counter = 1;
      foreach ($row1 as $field => $value){

      if (strpos($field, "charge") && !empty($value)) {

      print "$counter. $value<br />";
      $counter++;

      }//end if (strpos($field, "charge")

      }//end foreach ($row1 as $field => $value)
      [/CODE]


      It seems to me that you have allocated certain number of fields to write the charges. This may cause you a problem in the future as it will not let you scale.
      For example if you have 9 fields for chages but the offender was naughty 10 times.. where do you put the extra charge???

      Read up on Database normalisation and see how it can help you.

      Regards

      Comment

      Working...