db retrieval

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Andy128
    Major General

    • Dec 2005
    • 2317

    db retrieval

    I have a php display script that takes data from a flat file db and transforms it into an array and then displays that info in a table. No problems with this.

    Each line of data in the db contains 8 items. The first item is the genre or group identifier. For example:
    cars|2001|Dodge|Blue
    cars|2003|Chevy|Green
    truck|2006|Dodge|Black

    What I would like to do create a link to the display script. That link URL would contain or pass a variable (the genre / group identifier) to the display script that in turn displays only those items inside that genre or group.

    The display script is below:
    PHP Code:
    <html>
    <head>
    <style>
    .th{background:#ccdd88;}
    .td{background:#fffff0;}
    .tr{text-align:right;}
    .tc{text-align:center;}
    .pr{padding-right: 6px;}
    .plr{padding-left:6px;padding-right:6px;}
    .w1{width:100px;}
    .w2{width:75px;}
    .w3{width:400px}
    </style>
    <body>
    <?php
    ///////////////////////////////////////////////////////////
    //Here I assume I would declare the genre variable that is passed
    //in the URL from the link ???
    ///////////////////////////////////////////////////////////
    if(!file_exists("db_display_test.txt"))
    {
    echo
    "<center><b>NO RECORDS AT THIS TIME</center></b><br/>
    <center>CHECK BACK LATER</center>"
    ;
    exit;
    }
    echo
    "<table border=6 width=100% cellpadding=2 cellspacing=6>";
    echo
    "<tr>";
    echo
    "<th class='th plr'>Box 1</th>";
    echo
    "<th class='th plr'>Box 2</th>";
    echo
    "<th class='th plr'>Box 3</th>";
    echo
    "<th class='th plr'>Box 4</th>";
    echo
    "<th class='th plr'>Box 5</th>";
    echo
    "<th class='th plr'>Box 6</th>";
    echo
    "<th class='th plr'>Box 7</th>";
    echo
    "</tr>";
    //
    $openedfile fopen"db_display_test.txt"'r' );
    if(
    $openedfile)
    {
    while (!
    feof$openedfile ) )
        {
            
    $line trim(fgets$openedfile ));
            if ( !empty( 
    $line ) )
            {
                list( 
    $genre,$entry1,$entry2$entry3$entry4$entry5$entry6$entry7) = explode"|"$line );
    /////////////////////////////////////////////////////////////
    //What would go here to list only those entries that matched
    //a specific genre?
    ////////////////////////////////////////////////////////////
    echo"<tr>";
    echo
    "<td class='td tc w2'>$entry1</td>";
    echo
    "<td class='td tc w1'>$entry2</td>";
    echo
    "<td class='td tc w2'>$entry3</td>";
    echo
    "<td class='td tc w2'>$entry4</td>";
    echo
    "<td class='td tc w2'>$entry5</td>";
    echo
    "<td class='td tc w2'>$entry6</td>";
    echo
    "<td class='td tc w2'>$entry7</td>";
    echo
    "</tr>";
    }
    }
    fclose$openedfile );
    }
    else
    {
    echo
    "<tr>";
    echo
    "<td class='th w3'>Cannot open file!</td>";
    echo
    "</tr>";
    echo
    "</table>";
    }
    ?>
    </BODY>
    </HTML>
    That is my dilemma.

    Andy
    PHP- is a blast!
  • Andy128
    Major General

    • Dec 2005
    • 2317

    #2
    Re: db retrieval

    Apparently there are issues with the forum as I could not edit my above post.

    Note- I have only listed 4 items for brevity in the db example. Additionally- in the display script, I have not displayed the genre in the table on purpose.

    Andy
    PHP- is a blast!

    Comment

    • navaldesign
      General & Forum Moderator

      • Oct 2005
      • 12080

      #3
      Re: db retrieval

      Hi Andy,



      PHP Code:
      <html>
      <head>
      <style>
      .th{background:#ccdd88;}
      .td{background:#fffff0;}
      .tr{text-align:right;}
      .tc{text-align:center;}
      .pr{padding-right: 6px;}
      .plr{padding-left:6px;padding-right:6px;}
      .w1{width:100px;}
      .w2{width:75px;}
      .w3{width:400px}
      </style>
      <body>
      <?php
      // Receiving the asked for genre, through a form in a previous page
      @$askedforgenre addslashes($_POST['askedforgenre']);
       
      if(!
      file_exists("db_display_test.txt"))
      {
      echo
      "<center><b>NO RECORDS AT THIS TIME</center></b><br/>
      <center>CHECK BACK LATER</center>"
      ;
      exit;
      }
      echo
      "<table border=6 width=100% cellpadding=2 cellspacing=6>";
      echo
      "<tr>";
      echo
      "<th class='th plr'>Box 1</th>";
      echo
      "<th class='th plr'>Box 2</th>";
      echo
      "<th class='th plr'>Box 3</th>";
      echo
      "<th class='th plr'>Box 4</th>";
      echo
      "<th class='th plr'>Box 5</th>";
      echo
      "<th class='th plr'>Box 6</th>";
      echo
      "<th class='th plr'>Box 7</th>";
      echo
      "</tr>";
      //
      $openedfile fopen"db_display_test.txt"'r' );
      if(
      $openedfile)
      {
      while (!
      feof$openedfile ) )
          {
              
      $line trim(fgets$openedfile ));
              if ( !empty( 
      $line ) )
              {
                  list( 
      $genre,$entry1,$entry2$entry3$entry4$entry5$entry6$entry7) = explode"|"$line );
      /////////////////////////////////////////////////////////////
      //What would go here to list only those entries that matched
      //a specific genre?
      ////////////////////////////////////////////////////////////
      if ($genre == $askedforgenre) {
      echo
      "<tr>";
      echo
      "<td class='td tc w2'>$entry1</td>";
      echo
      "<td class='td tc w1'>$entry2</td>";
      echo
      "<td class='td tc w2'>$entry3</td>";
      echo
      "<td class='td tc w2'>$entry4</td>";
      echo
      "<td class='td tc w2'>$entry5</td>";
      echo
      "<td class='td tc w2'>$entry6</td>";
      echo
      "<td class='td tc w2'>$entry7</td>";
      echo
      "</tr>";
      }
      }
      }
      fclose$openedfile );
      }
      else
      {
      echo
      "<tr>";
      echo
      "<td class='th w3'>Cannot open file!</td>";
      echo
      "</tr>";
      echo
      "</table>";
      }
      ?>
      </BODY>
      </HTML>
      $askedforgenre of course can be changed to any variable name you like. A simple "if" statement is enough to only display those listings that match the required genre.
      Navaldesign
      Logger Lite: Low Cost, Customizable, multifeatured Login script
      Instant Download Cart: a Powerfull, Customized, in site, DB driven, e-products Cart
      DBTechnosystems.com Forms, Databases, Shopping Carts, Instant Download Carts, Loggin Systems and more....
      Advanced BlueVoda Form Processor : No coding form processor! Just install and use! Now with built in CAPTCHA!

      Comment

      • Andy128
        Major General

        • Dec 2005
        • 2317

        #4
        Re: db retrieval

        Naval- YOU ROCK! That was incredibly simple and it works perfectly.

        Thanks again!

        Reason: Currently I have about 125 pages each that lists businesses. So for Alarm Companies one would go to alarm_companies.html. But- I can enter all the businesses in a db and then use one page with the php script to get only those businesses that pertain to those that were requested. Thus effectively allowing me to have only two pages that serve up hundreds of pages worth of data. I hope you get what I mean.

        I also did this: I am making a page (scrollable) with all the links to the various businesses / services for my directory. The link is made like this;
        <a href="my_db.php?pg_genre=alarm_companies">Alarm Companies</a><br />
        And in the script I have:
        $pg_genre = $_GET['pg_genre'];

        That way I do not need a form for them to fill out.

        Very excited. I am going to be up all night!

        Talk to ya later and thanks again.
        Andy
        PHP- is a blast!

        Comment

        • navaldesign
          General & Forum Moderator

          • Oct 2005
          • 12080

          #5
          Re: db retrieval

          Yes, that is a very common way of passing values from a page to another. I use it myself all the time. I also use forms, if more than one values are to be passed (in that case i use database driven independent fieds or dependent (chained) dropdowns).Well done Andy!
          Navaldesign
          Logger Lite: Low Cost, Customizable, multifeatured Login script
          Instant Download Cart: a Powerfull, Customized, in site, DB driven, e-products Cart
          DBTechnosystems.com Forms, Databases, Shopping Carts, Instant Download Carts, Loggin Systems and more....
          Advanced BlueVoda Form Processor : No coding form processor! Just install and use! Now with built in CAPTCHA!

          Comment

          • Andy128
            Major General

            • Dec 2005
            • 2317

            #6
            Re: db retrieval

            Naval-
            I also made the table scrollable and placed that code inside an html box. In this manner- it is more SE friendly than an I-Frame containing the table.

            Have a look at this site. I believe it will be a good resource to others that you help along the way.
            Create scrolling divs for content on your webpages. Mobile friendly - use our code generator and send the code to your email.


            Lots of cool stuff on that site.

            Cheers-

            Andy
            PHP- is a blast!

            Comment

            Working...
            X