|
Posted by Hilarion on 10/13/17 11:24
skinnybloke <theoriginalsin73@yahoo.co.uk> wrote:
> Hi - I have the code below to display numbers and letters on a web
> page. The script queries a database and selects the first character of
> each company on there.
>
> If the character exists on the database it creates a link for that
> character, else it just displays the character.
>
> This works fince except it is creating a link for number "0" yet there
> are no company_name records beginning with "0". I currently only have
> 3 records on the database and all start with an alpha character yet
> the script is showing the numeric 0 as a linkable field.
>
> Does anybody know why this could be happening and how I could prevent
> it?
>
>
> <?php
>
> // This section generates the numbers and letters for the company
> search
>
> $query = 'SELECT DISTINCT LEFT(company_name,1) FROM members';
>
> $result = mysql_query($query);
> while($row = mysql_fetch_array($result))
> $alphanums[] = $row[0];
Try outputing contents of $alphanums array here to check if the
array contains what you need (and only this).
> for ($numbers = 0; $numbers <= 9; $numbers++) {
> if(in_array($numbers, $alphanums)) {
It may be a problem with types. You are searching for integer
in an array filled with strings. Maybe "in_array" function
casts the string to int to perform comparison and this way
zero is always in the array (cause all strings begining with
letters get converted to zero). Try changing the "if" expression
to:
if ( in_array( strval( $numbers ), $alphanums ) )
> echo "<a href='directory_new.php?coletter=$numbers'>$numbers </a>";
HTML tip: You are linking space character here. Change it to:
echo "<a href='directory_new.php?coletter=$numbers'>$numbers</a> ";
> } else {
> echo "$numbers ";
> }
> }
>
> echo '<br />';
> for ($alphabet1 = A; $alphabet1 <= M; $alphabet1++) {
You are using constants here (probably not defined constants, which
causes them to be converted to string literals) instead of string
literals. You probably should change it to:
for( $alphabet1 = 'A'; $alphabet1 <= 'M'; $alphabet1++ )
> if(in_array($alphabet1, $alphanums)) {
> echo "<a href='directory_new.php?coletter=$alphabet1'>$alphabet1 </a>";
As before:
echo "<a href='directory_new.php?coletter=$alphabet1'>$alphabet1</a> ";
> } else {
> echo "$alphabet1 ";
> }
> }
>
> for ($alphabet2 = N; $alphabet2 != AA; $alphabet2++) {
Same as before:
for ($alphabet2 = 'N'; $alphabet2 != 'AA'; $alphabet2++) {
I do not understand why are you splitting range 'A' - 'Z' to two ranges
'A' - 'M' and 'N' - 'Z'. Why use "$alphabet2 != 'AA'" instead
of "$alphabet2 <= 'Z'" ? Do you want also some other chars to get
checked?
> if(in_array($alphabet2, $alphanums)) {
> echo "<a href='directory_new.php?coletter=$alphabet2'>$alphabet2 </a>";
As before:
echo "<a href='directory_new.php?coletter=$alphabet2'>$alphabet2</a> ";
> } else {
> echo "$alphabet2 ";
> }
> }
> ?>
Hope I helped.
Hilarion
[Back to original message]
|