Jump to content

Help to improve code.


crusty76

Recommended Posts

I have been 'volunteered' to produce an online catalogue for a private library and Museum. I have some minimum knowledge of PHP and have produced the code below which works. However, I know it's very poor code and could be more effective and efficient. I would appreciae help on how to make improvements. Code below.

------------------------

<?PHP

$connect = odbc_connect('museum9', '', '');

 

 

if (!$connect)

  {exit("Connection *** Failed: " . $connect);}

 

$sql="SELECT * FROM BooksQuery";

$rs=odbc_exec($connect,$sql);

if (!$rs)

  {exit("Error *** in SQL");}

 

$b= "<a href=\"bookdets.php?";

$count=0;

 

while (odbc_fetch_row($rs))

{

$id = odbc_result($rs,"ID");

  $Title=odbc_result($rs,"Title");

$Author = odbc_result($rs,"Author");

$cat = odbc_result($rs,"CAT");

$cat = rtrim($cat);

 

 

if (stripos($Title . $Author, $search,0)!==FALSE) {

 

echo "<tr><td>".$Title."</td><td>".$Author."</td><td>". $b. $id."\">".$cat."</a></td></tr>";

 

 

 

$count++;

}

}

 

odbc_close($connect);

 

?>

Link to comment
Share on other sites

Here are my comments:

 

1. give your variables descriptive names. $b tells me nothing about what the value is supposed to be. Also, for $b, I would just define the base url and not the opening A tag. Then the resulting echo at the bottom would look proper.

 

2. The variable $count serves no purpose in that code.

 

3. It looks like you are querying all the records and then using PHP and stripos() to filter the results. You should just use the query to return only the records you want.

 

<?PHP
$connect = odbc_connect('museum9', '', '');

if (!$connect)
  {exit("Connection *** Failed: " . $connect);}

$sql = "SELECT ID, Title, Author, CAT
        FROM BooksQuery
        WHERE Title LIKE '{$search}%'
           OR Author LIKE '{$search}%'";
$rs=odbc_exec($connect,$sql);
if (!$rs)
  {exit("Error *** in SQL");}

$baseURL = "bookdets.php";

while (odbc_fetch_row($rs))
{
    $id     = odbc_result($rs,"ID");
    $Title  = odbc_result($rs,"Title");
    $Author = odbc_result($rs,"Author");
    $cat    = rtrim(odbc_result($rs,"CAT"));

    echo "<tr>\n";
    echo "<td>{$Title}</td>";
    echo "<td>{$Author}</td>";
    echo "<td><a href=\"{$b}?{$id}\">{$cat}</a></td>";
    echo "</tr>";
}

odbc_close($connect);

?>

Link to comment
Share on other sites

Thanks.

I didn't show the complete HTML page with a form input for a search string. Sorry about that.

If you have time, pleease have a look at

http://www.philbrick2255.org.uk/museum/books.php

 

It allows a search and reports the count of items found. I'm not a programmer as you can see. I'm just trying to do the best job I can for the Museum

Thanks

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.