Change (LIKE header 'A%') to B or C etc. (Rewrite)

This is a rewrite of a question that was closed because it was too vague and I entered the code incorrectly. Hope this makes sense.

I have a large SQL database containing "title" and "author". I created a request using PHP that will only select headers starting with A - (LIKE header 'A%') - on one page:

$result = mysql_query("SELECT * FROM table WHERE (title LIKE 'A%') ORDER BY title ASC");

//fetch the data from the database
while ($row = mysql_fetch_array($result)) {
$DirPath = $row{'filename'};
//To get the innermost dir 'display just mp3'
$InnermostDir = basename(rtrim($DirPath, '/'));

echo "<tr><td align='justify'>";
echo $row{'title'} ;
echo "</td><td align='center'>";
echo $row{'author'} ;
echo "</td><td align='center'>";
echo $row{'publisher'} ;
echo "</td><td align='center'>";
echo $row{'pages'} ;
echo "</td><td align='center'>";
echo $row{'year'} ;
echo "</td><td align='center'>";
echo $row{'type'} ;
echo "</td><td align='center'>";
echo $row{'genre'} ;
echo "</td><td align='center'>";
echo $row{'location'} ;
echo "</td><td align='center'>";
echo $row{'id'} ;
echo "</td><td align='center'>";
echo $row{'shortauthor'} ;
echo "</td></tr>";
}
?>
</table>
<?php
//close the connection
mysql_close($dbhandle);
?>

      

http://sanctuary-westminster.org/library/epa.php

For now, I just copied page 25 where the only difference is that the letter changes (and the page title changes to reflect the letter) and then just use a simple href = "epb.php" to join them together. From the epb.php page:

$result = mysql_query("SELECT * FROM table WHERE (title LIKE 'B%') ORDER BY title ASC");

      

http://sanctuary-westminster.org/library/epb.php

This is a very long and unattractive approach, any changes require changing all pages. I've tried using $ letter instead of link and LIKE 'A%' LIKE '$ letter%' instead:

$result = mysql_query("SELECT * FROM table WHERE (title LIKE 'row{'letter'}%') ORDER BY title ASC");

      

But this fails; it gives no error messages, no request occurs because the input value is incorrect, so nothing is returned. Hope this will be better and apologize to.

(I also looked at ways to use the dropdown menu where the Submit command stores the letter selected from the options and then it is used in the query, but when I look at the dropdowns and SQL, link to populate the menu with a table without creating a query using one . Not sure if this will work better)

+3


source to share


1 answer


You have one page titled lib.php

and pass an argument to it likelib.php?letter=A

Change your code to:



$result = mysql_query("SELECT * FROM table WHERE (title LIKE '" . mysql_real_escape_string($_GET['letter']) . "%') ORDER BY title ASC");

      

Now the same file lib.php

,, can generate all the pages previously.

+3


source







All Articles