/    Sign up×
Community /Pin to ProfileBookmark

PHP Mysqli A to Z index

I am trying to port my code from PHP/MySQL to MySQLi but cant seem to get it to work, can any body see where I am going wrong??

The Query Works OK but when I try to output the index all buttons are active, when only the button with the letters from the SELECT result should be active.

Using latest PHP, MySQLi and BootStrap

[CODE]<?php

/* SELECT DISTINCT FIRST LETTERS*/

$sqll=”SELECT DISTINCT LEFT(lnk_topic, 1) as letter FROM `lnk_topics` ORDER BY letter”;
if(!$resultl = $mysqli->query($sqll)){
die(‘There was an error running the query [‘ . $mysqli->error . ‘]’);
}

/* PERFORM QUERY */
$rowl = $resultl->fetch_array();
/* CONVERT ARRAY TO UPPERCASE FOR COMPARISON*/
$larray=array_map(“strtoupper”, $rowl);

/*LOOP THRU EACH LETTER OF THE ALPHABET */
foreach (range(‘A’, ‘Z’) as $char) {

if(in_array($char,$larray)) {
echo “<a href=’#”.strtoupper($char).”‘ class=’btn btn-info’ role=’button’>”.strtoupper($char).”</a>n”;
}
else
{
echo “<a href=’#’ class=’btn btn-sm btn-disabled’ role=’button’>”.strtoupper($char).”</a>n”;
}
}

echo “<hr>n”;

?>[/CODE]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 19.2016 — Before we go too far: have you checked what actually ends up in $larray ?

[code=php]
$larray=array_map("strtoupper", $rowl);
echo "<pre>DEBUGn".print_r($larray, 1)."</pre>n";
[/code]
Copy linkTweet thisAlerts:
@Zaaka1972authorMay 20.2016 — I have added the 'Debug' line after the $rowl line and the $larray line and got the following:

[CODE]
DEBUG 'Rowl'
Array
(
[0] => a
[letter] => a
)

DEBUG 'Larray'
Array
(
[0] => A
[letter] => A
)
[/CODE]
Copy linkTweet thisAlerts:
@Zaaka1972authorMay 20.2016 — I have added the debug after the Query is executed and got the following:

[CODE]
DEBUG 'Resultl'
mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 15
[type] => 0
)
[/CODE]
Copy linkTweet thisAlerts:
@NogDogMay 20.2016 — Okay, a couple things. I tested the display code with the following:
[code=php]
$larray = array('A', 'E', 'I', 'O', 'U');
/*LOOP THRU EACH LETTER OF THE ALPHABET */
foreach (range('A', 'Z') as $char) {

if(in_array($char,$larray)) {
echo "<a href='#".strtoupper($char)."' class='btn btn-info' role='button'>".strtoupper($char)."</a>n";
}
else
{
echo "<a href='#' class='btn btn-sm btn-disabled' role='button'>".strtoupper($char)."</a>n";
}
}
[/code]

When I look at the output, it matches what you appear to want:
[code=html]
<a href='#A' class='btn btn-info' role='button'>A</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>B</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>C</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>D</a>
<a href='#E' class='btn btn-info' role='button'>E</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>F</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>G</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>H</a>
<a href='#I' class='btn btn-info' role='button'>I</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>J</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>K</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>L</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>M</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>N</a>
<a href='#O' class='btn btn-info' role='button'>O</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>P</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>Q</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>R</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>S</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>T</a>
<a href='#U' class='btn btn-info' role='button'>U</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>V</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>W</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>X</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>Y</a>
<a href='#' class='btn btn-sm btn-disabled' role='button'>Z</a>
[/code]

So...see if the raw HTML is what you expected, and if so, then maybe the issue is something to do with your client side library to handle the class and role attribute values.

On another level, your code as written will only grab the first result row of the query. You would need to loop through all the result rows, maybe something like:
[code=php]
<?php

/* SELECT DISTINCT FIRST LETTERS*/

$sqll="SELECT DISTINCT LEFT(lnk_topic, 1) as letter FROM lnk_topics ORDER BY letter";
if(!$resultl = $mysqli->query($sqll)){
die('There was an error running the query [' . $mysqli->error . ']');
}

$larray = array();
while(($rowl = $resultl->fetch_array()) != false) {
$larray[] = strtoupper($rowl[0]);
}

/*LOOP THRU EACH LETTER OF THE ALPHABET */
foreach (range('A', 'Z') as $char) {

if(in_array($char,$larray)) {
echo "<a href='#".strtoupper($char)."' class='btn btn-info' role='button'>".strtoupper($char)."</a>n";
}
else
{
echo "<a href='#' class='btn btn-sm btn-disabled' role='button'>".strtoupper($char)."</a>n";
}
}


echo "<hr>n";


?>
[/code]
Copy linkTweet thisAlerts:
@Zaaka1972authorMay 20.2016 — That's Great, Thanks NogDog, i new i was missing something.
×

Success!

Help @Zaaka1972 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...