tags:

views:

205

answers:

1

Hello All,

I am new... to the site and to PHP in general. Please forgive my ineptitude.

Anyway, I tried poring over the tagged posts for "passing" "variables" etc. Got some bites, but not exactly what I need. Unfortunately, I learn things by reverse engineering, so like many, I've been going around stealing snippets of PHP and adapting to my needs. Makes for some huge gaps in the learning process that I need to fill in.

Anyway, I have a page that I put together and it's pulling a bunch of nicely ordered records from the DB I made. I have them displaying the way I like using ASC in the query.

I have navigation to filter those results by first letter like "../name.php?bbname=a" but due to my incompetence, it doesn't work.

I think the method I have for constructing the query may be too inflexible?? I am including my junky code below with commented out areas to show what I'm trying to get at. I donm't know why I have two queries included. It's as if I have one sitting there just "waiting" for the passed info. I assume I should just re-work the original query to lay in waiting for the GET stuff yet to still put together the large SELECT ALL

Sorry if my question posing methods are off... THANK YOU!

<?php include "header.php";?>
    <?php include "wrap.php";?>
    <?php include "left.php";?>
    <div id="content">
      <div id="ad728x90">
        <?php include "ad728x90.php";?>
      </div>
      <?php include "utilplaces.php";?>
      <h1>Places by Name</h1>
      <div id="horizon"><a href="?bbname=a">A</a> <a href="?bbname=b">B</a> <a href="?bbname=c">C</a> <a href="?bbname=d">D</a> <a href="?bbname=e">E</a> <a href="?bbname=f">F</a> <a href="?bbname=g">G</a> <a href="?bbname=h">H</a> <a href="?bbname=i">I</a> <a href="?bbname=j">J</a> <a href="?bbname=k">K</a> <a href="?bbname=l">L</a> <a href="?bbname=m">M</a> <a href="?bbname=n">N</a> <a href="?bbname=o">O</a> <a href="?bbname=p">P</a> <a href="?bbname=q">Q</a> <a href="?bbname=r">R</a> <a href="?bbname=s">S</a> <a href="?bbname=t">T</a> <a href="?bbname=u">U</a> <a href="?bbname=v">V</a> <a href="?bbname=w">W</a> <a href="?bbname=x">X</a> <a href="?bbname=y">Y</a> <a href="?bbname=z">Z</a> <a href="?bbname=0">0-9</a></div>
      <div class="cnp"></div>
    </div>
    <div id="biggun">

    <?php 
    // Connects to the Database 
    mysql_connect("localhost","user","password") or die(mysql_error());

    mysql_select_db("databasename") or die(mysql_error());

    // I AM TRYING TO GRAB THE VARIABLES FROM THE URL BUT HAVE NO CLUE
    $bbname = $_GET['bbname'];

    // THIS IS THE STANDARD DB QUERY TO SETUP THE PAGE IN DEFAULT LOAD
    $data = mysql_query("SELECT * FROM places ORDER BY `places`.`name` ASC LIMIT 0, 30 ")
    or die(mysql_error());



    echo "<table id=\"placesstable\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
    echo "    <col id=\"bbNAME\" />\n"; 
    echo "    <col id=\"bbIMG\" />\n"; 
    echo "    <col id=\"bbDETAILS\" />\n"; 
    echo "    <col id=\"bbHOURS\" />\n"; 
    echo "    <col id=\"bbTYPE\" />\n"; 
    echo "    <col id=\"bplacesEA\" />\n"; 
    echo "    <col id=\"bbRATING\" />\n"; 
    echo "    <thead>\n"; 
    echo "      <tr>\n"; 
    echo "        <th style=\"border: none;\">Name</th>\n"; 
    echo "        <th>&nbsp;</th>\n"; 
    echo "        <th>Details </th>\n"; 
    echo "        <th>Hours</th>\n"; 
    echo "        <th>Type</th>\n"; 
    echo "        <th>Area</th>\n"; 
    echo "        <th> Rating</th>\n"; 
    echo "      </tr>\n"; 
    echo "    </thead>\n";
    echo "    <tbody>\n";
    while($info = mysql_fetch_array( $data )) 
    { 
    echo "<tr>"; 
    echo "<td><p class=\"placesnametable\"><a href=\"#\">".$info['name'] . "</a></p></td> ";
    echo "<td><img src=\"".$info['tmbimg']."\" alt=\"\" name=\"placesthumb\" width=\"100\" height=\"67\" class=\"placesthumb\" /></td> ";
    echo "<td>".$info['address'] . "<br />\n" .$info['phonenumber'] . "</td>"; 
    echo "<td>".$info['hours'] . "</td> "; 
    echo "<td>".$info['type'] . "</td> "; 
    echo "<td>".$info['district'] . "</td> "; 
    echo "<td>Pending<!--RATING--></td></tr>"; 
    } 
    echo "</tbody>"; 
    echo "</table>"; 

    ?>
    </div>
    <?php include "feetie.php";?>
A: 

Try this:

    if(isset($_GET['bbname'])) {
      $name = mysql_real_escape_string($_GET['bbname']);
$query = mysql_query("SELECT * FROM places ORDER BY name ASC LIMIT 0,30 WHERE name LIKE '$name'") OR die(mysql_error());
    }
    else {
       $data = mysql_query("SELECT * FROM places ORDER BY `places`.`name` ASC LIMIT 0, 30 ")
    }

Instead of what you had at that block.

Kevin Korb
Kevin,Thanks for working with my mess. Almost there. The ELSE bit works fine as the page renders OK when no bbname is specifed. Once I click a link, the page renders again, but with no results.I suspect something is not making its way to the LIKE % bit of the query?ThanksRob
rob - not a robber
You should be using backticks (`) in the LIKE part of the query, instead of straight quotes (').
BlackAura
Aura, is that just a good practices measure? In anycase, I keep getting this when I try passing the variableFatal error: Call to undefined function mysql_real_escape_data() in /home/myaccount/public_html/sitename/name.php on line 30
rob - not a robber
@BlackAura What? Backticks are for escaping field and table names in MySql. Not for values.
troelskn
I fixed it. ignore the crappy indentation.
Kevin Korb
Try " .... WHERE name LIKE '%$name%'" for a fuzzy mysql search. '%' is the mysql wildcard characte.
too much php
Thanks all... I know I'm almost there... just gotta tweak some things.I'm now getting You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE name LIKE \'e%\'' at line 1At least I know the values are passing through.
rob - not a robber
Got it... called upon column names specifically instead of with the *. Also had to name the variable "query" to "data" to be utilized down below.THANKS AGAIN ALL!
rob - not a robber