SitePoint Sponsor

User Tag List

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

    Need Help Tweaking Database Table PHP

    I just published a page online at http://www.geoworld.org/north_america/usa/az/counties/ The sortable database tables work great, but I had to do some heavy tweaking, and I inherited a couple problems in the process.

    First, notice the rows n\n\n\n\n\n\n\ on top of each table. The original script ends each table like this:

    PHP Code:
    </tr>\n"; 
    I had to reverse all the quotes in my revised table, so I had to change the end to this:

    PHP Code:
    </tr>\n'; 
    So why does it print n's across the page? (And what's the slash n for in the first place?)

    The second problem is that my dynamic table row colors stopped working. The problem's almost certainly somewhere in here:

    PHP Code:
    //<!-- BeginDynamicTable -->
    $rowcounter=0;
    while (
    $row mysql_fetch_array ($res)) {
         
    $c=$colors[$rowcounter++%$size];

    echo 
    '<tr style=\"background-color:$c\"><'$_SERVER["PHP_SELF"] ."?id="$row["CCode"] .'> 
    Again, I reversed all the quotes, so that's probably the problem, but I haven't yet been able to make it work again. Any tips? I posted the entire source code below.

    Thanks for any tips.

    PHP Code:
    <?php
    [DATABASE CONNECTION]
    $mycode 'us-az';
    $foo = array("us-""ca-");
    $mycode str_replace($foo""$mycode);
    ?>
    <?php 
    /* if ( $mytopic == 'counties' ) include($_SERVER['DOCUMENT_ROOT'] . '/includes/counties/' . $data[0]['IDParent'] . '.php'); */ ?>
    <h2>County Basics</h2>

    <div class="formdiv">
      <form action="index.php" method="GET">
        <select name="order">
          <option value="1">County</option>
          <option value="2">County Seat</option>
          <option value="3">Area (Square Miles)</option>
          <option value="4">Area (KM)</option>
          <option value="5">Population Density (Sq Mi)</option>
          <option value="6">Pop Dens (KM)</option>
          <option value="7">Population (2000)</option>
          <option value="8">Population (1990)</option>
        </select>
        <input type="radio" name="direction" value="0">
        Ascending
        <input type="radio" name="direction" value="1">
        Descending
        <input type="submit" name="submit" value="Submit">
      </form>
    </div>
    <?php
    $colors 
    = array( '#eee''''#ff9''''#cff''''#cfc''' );
    $n=0;
    $size=count($colors);

    $result mysql_query('select count(*) from counties');
    if ((
    $result) && (mysql_result ($result 0) > 0)) {
    // continue here with the code that starts
    //$res = mysql_query ("SELECT * FROM type.....
    } else {
    die(
    'Invalid query: ' mysql_error());
    }
    {
    $order = isset($_REQUEST['order']) ? intval($_REQUEST['order']) : 0;
    switch(
    $order)
    /* The above line replaced thie following, which gave me an error message:
      switch(intval($_REQUEST['order'])) */
    {
       case 
    1:
           
    $order 'NameC';
           break;
       case 
    2:
           
    $order 'Seat';
           break;
       case 
    3:
           
    $order 'Area_MI';
           break;
       case 
    4:
           
    $order 'Area_KM';
           break;
       case 
    5:
           
    $order 'Pop_MI';
           break;
       case 
    6:
           
    $order 'Pop_KM';
           break;
       case 
    7:
           
    $order 'Pop_2000';
           break;
       case 
    8:
           
    $order 'Pop_1990';
           break;
       case 
    9:
       default:
           
    $order 'NameC';
           break;
    }
    if (isset(
    $_REQUEST['direction']) && intval($_REQUEST['direction'])) {
    // if (isset($_REQUEST['direction']) && intval($_REQUEST['direction'])) {
    $direction ' DESC';
    } else {
        
    $direction '';
    }
    }

    // if type contains content then print out list
    $result mysql_query('select count(*) from counties');
    if ((
    $result) && (mysql_result ($result 0) > 0)) {
    } else {
    die(
    'Invalid query: ' mysql_error());
    }
    {

     
    $res mysql_query ("SELECT * from counties
     WHERE SCode = 'us-az'
     ORDER BY counties." 
    $order $direction) or die (mysql_error()); 

    echo 
    '<table class="sortphp" id="tab_counties1">
    <thead>
    <tr><th>Name</th><th>Seat</th><th>Area</th><th>Area(km)</th><th>Dens</th><th>Dens(km)</th><th>Pop(2000)</th><th>Pop(1990)</th></tr>
    </thead>
             <tbody>'
    ;
    //<!-- BeginDynamicTable -->

    while ($row mysql_fetch_array ($res))
    {

    //<!-- BeginDynamicTable -->
    $rowcounter=0;
    while (
    $row mysql_fetch_array ($res)) {
         
    $c=$colors[$rowcounter++%$size];

    echo 
    '<tr style=\"background-color:$c\"><'$_SERVER["PHP_SELF"] ."?id="$row["CCode"] .'>
    <td class="tdname">'
    $row["NameC"] .'</td>
    <td class="tdseat">'
    $row["Seat"] .'</td>
    <td class="tdarea">'
    number_format($row["Area_MI"]) .'</td>
    <td class="tdareakm">'
    number_format($row["Area_KM"]) .'</td>
    <td class="tddens">'
    number_format($row["Pop_MI"]) .'</td>
    <td class="tddenskm">'
    number_format($row["Pop_KM"]) .'</td>
    <td class="tdpop">'
    number_format($row["Pop_2000"]) .'</td>
    <td class="tdpop2">'
    number_format($row["Pop_1990"]) .'</td>
             </tr>\n'
    ; }
    }
    }
    ?>
    </tbody></table>




    // SECOND TABLE - Pretty similar to the first...



    <!-- NEW TABLE-->
    <a name="Race"></a>
    <h2>Race</h2>

    <div class="formdiv">
      <form action="index.php" method="GET">
        <select name="order">
          <option value="1">County</option>
          <option value="2">Amerindian</option>
          <option value="3">White</option>
          <option value="4">Black</option>
          <option value="5">Asian</option>
          <option value="6">Pacific_Island</option>
          <option value="7">Some_Other_Race</option>
          <option value="8">Race2</option>
          <option value="9">Hispanic</option>
        </select>
        <input type="radio" name="direction" value="0">
        Ascending
        <input type="radio" name="direction" value="1">
        Descending
        <input type="submit" name="submit" value="Submit">
      </form>
    </div>

    <?php
    $colors 
    = array( '#eee''''#ff9''''#cff''''#cfc''' );
    $n=0;
    $size=count($colors);

    {
    $order = isset($_REQUEST['order']) ? intval($_REQUEST['order']) : 0;
    switch(
    $order)
    {
       case 
    1:
           
    $order 'NameC';
           break;
       case 
    2:
           
    $order 'Amerindian';
           break;
       case 
    3:
           
    $order 'White';
           break;
       case 
    4:
           
    $order 'Black';
           break;
       case 
    5:
           
    $order 'Asian';
           break;
       case 
    6:
           
    $order 'Pacific_Island';
           break;
       case 
    7:
           
    $order 'Some_Other_Race';
           break;
       case 
    8:
           
    $order 'Race2';
           break;
       case 
    9:
           
    $order 'Hispanic';
           break;
       case 
    10:
       default:
           
    $order 'NameC';
           break;
    }
    if (isset(
    $_REQUEST['direction']) && intval($_REQUEST['direction'])) {
    // if (isset($_REQUEST['direction']) && intval($_REQUEST['direction'])) {
    $direction ' DESC';
    } else {
        
    $direction '';
    }
    }

    // if type contains content then print out list
    $result mysql_query('select count(*) from counties');
    if ((
    $result) && (mysql_result ($result 0) > 0)) {
    } else {
    die(
    'Invalid query: ' mysql_error());
    }
    {

     
    $res mysql_query ("SELECT * from counties
     WHERE SCode = 'us-az'
     ORDER BY counties." 
    $order $direction) or die (mysql_error()); 

    echo 
    '<table class="sortphp" id="tab_counties2">
             <thead>
                <tr><th>County</th><th>Native</th><th>White</th><th>Black</th><th>Asian</th><th>PI</th><th>Other</th><th>Race2</th><th>Hisp</th></tr>
             </thead>
             <tbody>'
    ;
    //<!-- BeginDynamicTable -->

    while ($row mysql_fetch_array ($res))
    {

    //<!-- BeginDynamicTable -->
    $rowcounter=0;
    while (
    $row mysql_fetch_array ($res)) {
         
    $c=$colors[$rowcounter++%$size];

    echo 
    '<tr style=\"background-color:$c\"><'$_SERVER["PHP_SELF"] ."?id="$row["CCode"] .'>
         <td class="tdname">'
    $row["NameC"] .'</td>
         <td class="tdnative">'
    number_format($row["Amerindian"]) .'</td>
         <td class="tdwhite">'
    number_format($row["White"]) .'</td>
         <td class="tdblack">'
    number_format($row["Black"]) .'</td>
         <td class="tdasian">'
    number_format($row["Asian"]) .'</td>
         <td class="tdpi">'
    number_format($row["Pacific_Island"]) .'</td>
         <td class="tdsor">'
    number_format($row["Some_Other_Race"]) .'</td>
         <td class="td2">'
    number_format($row["Race2"]) .'</td>
         <td class="tdhisp">'
    number_format($row["Hispanic"]) .'</td>
             </tr>\n'
    ; }
    }
    }
    ?>
    </tbody></table>

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    \n
    but why u use style on tr ????
    i noticed u once about your echos
    echo "bla";
    or
    echo ("bla");
    cheers


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
  •