crusty76 Posted May 13, 2010 Share Posted May 13, 2010 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/201656-help-to-improve-code/ Share on other sites More sharing options...
Psycho Posted May 13, 2010 Share Posted May 13, 2010 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/201656-help-to-improve-code/#findComment-1057855 Share on other sites More sharing options...
crusty76 Posted May 13, 2010 Author Share Posted May 13, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/201656-help-to-improve-code/#findComment-1057864 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.