PHP/SQL/Wordpress: Group a user list by alphabet - php

I want to create a (fairly big) Wordpress user index with the users categorized alphabetically, like this:
A
Amy
Adam
B
Bernard
Bianca
and so on.
I've created a custom Wordpress query which works fine for this, except for one problem: It also displays "empty" letters, letters where there aren't any users whose name begins with that letter. I'd be glad if you could help me fix this code so that it only displays the letter if there's actually a user with a name of that letter :) I've tried my luck by checking how many results there are for that letter, but somehow that's not working.
(FYI, I use the user photo plugin and only want to show users in the list who have an approved picture, hence the stuff in the SQL query).
<?php
$alphabet = range('A', 'Z');
foreach ($alphabet as $letter) {
$user_count = $wpdb->get_results("SELECT COUNT(*) FROM wp_users WHERE display_name LIKE '".$letter."%' ORDER BY display_name ASC");
if ($user_count > 0) {
$user_row = $wpdb->get_results("SELECT wp_users.user_login, wp_users.display_name
FROM wp_users, wp_usermeta
WHERE wp_users.display_name LIKE '".$letter."%'
AND wp_usermeta.meta_key = 'userphoto_approvalstatus'
AND wp_usermeta.meta_value = '2'
AND wp_usermeta.user_id = wp_users.ID
ORDER BY wp_users.display_name ASC");
echo '<li class="letter">'.$letter.'';
echo '<ul>';
foreach ($user_row as $user) {
echo '<li>'.$user->display_name.'</li>';
}
echo '</ul></li>';
}
}
?>
Thanks in advance!
Update:
I edited the SQL into this, but now I have the problem that it spits out an </ul> before the first li, which screws up the layout, like this:
</ul>
</li>
<li class="letter">
<div class="letter_head">A</div>
<ul>
<li>Abigail</li>
</ul>
</li>
<li class="letter">
<div class="letter_head">B</div>
<ul>
<li>Bernard</li>
<li>Bianca</li>
</li>
</ul>
It's in general pretty wonky about the ul and li nesting. How can I fix it?
<?php
$qry = "SELECT DISTINCT wp_users.user_login, wp_users.display_name,
LEFT(UPPER(wp_users.display_name), 1) AS first_char
FROM wp_users, wp_usermeta
WHERE UPPER(wp_users.display_name) BETWEEN 'A' AND 'Z'
OR wp_users.display_name BETWEEN '0' AND '9'
AND wp_usermeta.meta_key = 'userphoto_approvalstatus'
AND wp_usermeta.meta_value = '2'
AND wp_usermeta.user_id = wp_users.ID
ORDER BY wp_users.display_name ASC";
$result = mysql_query($qry);
$current_char = '';
while ($row = mysql_fetch_assoc($result)) {
if (!isset($current_char)) {
echo '<li class="letter"><div class="letter_head">'.$current_char.'</div>';
echo '<ul>';
} elseif ($row['first_char'] != $current_char) {
echo '</ul>';
$current_char = $row['first_char'];
echo '<li class="letter"><div class="letter_head">'.$current_char.'</div>';
echo '<ul>';
}
echo '<li>'.$row['display_name'].'</li>';
}
?>

A better solution would be to loop through all users in alphabetical order and for every record check if the first letter differs from that of the last record. If different, you echo out the new letter.

You are doing 52 database calls. Is the user table too big to grab all of them in one query? Also, I believe the order by on the count is unnecessary. I do not know if MySQL ignores it or not.
Your problem is get_results returns an single element array every time for the count query. So $user_count > 0 is always returning true. (I don't understand PHP logic most of the time. Is an array greater than zero?). You need to do something like $user_count[0] to get at the actual number.

Related

Joining tables and subqueries in MySQL

I know this question has been asked 1000s of times before but please bear with me as I have a slightly different scenarion.
Basically I have 2 MYSQL tables that I join (one called category and the other is products) and I create categories and subcategories menu in my PHP page using the following code:
<?php
$subcategories="";
$sql3="SELECT products.di_cat, GROUP_CONCAT(products.di_name) as di_name
FROM products LEFT JOIN category ON products.di_cat=category.names
GROUP BY products.di_cat";
$query3 = mysqli_query($db_conx, $sql3);
$existCount3 = mysqli_num_rows($query3);
if ($existCount3!=0) {
while($row3 = mysqli_fetch_array($query3, MYSQLI_ASSOC)){
$make_names = $row3["di_cat"];
$make_model = $row3["di_name"];
$subcategories = explode(",",$make_model);
print '<div class="col-md-3">
<span class="mega-menu-sub-title">'.$make_names.'</span>
<ul class="sub-menu">
<li>
<ul class="sub-menu">';
foreach($subcategories as $sub){
print '<li>'.$sub.'</li>';
}
print '</ul></li></ul></div>';
}
}
?>
This works fine and as it should.
Now I have a column in my products table called url.
This column is for creating clean URLs and the content of is basically the products names without any spaces or special characters.
I would like to use this in my code above but I don't understand why every time I try to use the follwoing code, I only get 1 subcategory/product from each category being displayed in my PHP page even though I have 100s of products under each category!
This what i tried:
<?php
$subcategories="";
$sql3="SELECT products.di_cat, products.url, GROUP_CONCAT(products.di_name) as di_name
FROM products LEFT JOIN category ON products.di_cat=category.names
GROUP BY products.di_cat";
$query3 = mysqli_query($db_conx, $sql3);
$existCount3 = mysqli_num_rows($query3);
if ($existCount3!=0) {
while($row3 = mysqli_fetch_array($query3, MYSQLI_ASSOC)){
$make_names = $row3["di_cat"];
$make_model = $row3["di_name"];
$make_url = $row3["url"];
$subcategories = explode(",",$make_url);
print '<div class="col-md-3">
<span class="mega-menu-sub-title">'.$make_names.'</span>
<ul class="sub-menu">
<li>
<ul class="sub-menu">';
foreach($subcategories as $sub){
print '<li>'.$sub.'</li>';
}
print '</ul></li></ul></div>';
}
}
?>
Can someone please advice on this issue?
Any help would be appreciated.

Php grouping select query

So I have persons associated with publications
My current query is this:
$query = "SELECT confAuth.personid, publicationconf.title FROM confAuth INNER JOIN publicationconf ON publicationconf.conferenceid = confAuth.conferenceid GROUP BY publicationconf.conferenceid";
It does group by conference id but only one person id is diplayed for each entry. Obviously, what I need is to display all person id's
Thanks in advance.
As a classmate just noted, this is our print statement and it is probably wrong. Any input to this.
$result = $conn->query($query);
$rows = $result->num_rows;
if ($rows > 0)
{
for($i=0; $i<$rows; $i++)
{
if($row = $result->fetch_assoc())
{
?>
<div class="row">
<div class="col-md-10">
<p><?php echo $row["personid"] . ", " . $row["title"]?></p>
Use group_concat(confAuth.personid) to get a comma delimited list of all values in the grouped set.
However, since you are wanting one line item per personid/title combo, add the personid and the title to the group by clause.
Also, consider table aliases for readability.
$query = "SELECT ca.personid, pc.title
FROM confAuth ca
INNER JOIN publicationconf pc ON pc.conferenceid = ca.conferenceid
GROUP BY pc.conferenceid, ca.personid, pc.title";

Inefficient SQL Query

I'm building a simple web app at the moment that I'll one day open source. As it stands at the moment, the nav is generated on every page load (which will change to be cached one day) but for the moment, it's being made with the code below. Using PHP 5.2.6 and MySQLi 5.0.7.7, how more efficient can the code below be? I think joins might help, but I'm after advice. Any tips would be greatly appreciated.
<?php
$navQuery = $mysqli->query("SELECT id,slug,name FROM categories WHERE live=1 ORDER BY name ASC") or die(mysqli_error($mysqli));
while($nav = $navQuery->fetch_object()) {
echo '<li>';
echo ''. $nav->name .'';
echo '<ul>';
$subNavQuery = $mysqli->query("SELECT id,name FROM snippets WHERE category='$nav->id' ORDER BY name ASC") or die(mysqli_error($mysqli));
while($subNav = $subNavQuery->fetch_object()) {
echo '<li>';
echo ''. $subNav->name .'';
echo '</li>';
}
echo '</ul>';
echo '</li>';
}
?>
You can run this query:
SELECT c.id AS cid, c.slug AS cslug, c.name AS cname,
s.id AS sid, s.name AS sname
FROM categories AS c
LEFT JOIN snippets AS s ON s.category = c.id
WHERE c.live=1
ORDER BY c.name, s.name
Then iterate thru the results to create the proper heading like:
// last category ID
$lastcid = 0;
while ($r = $navQuery->fetch_object ()) {
if ($r->cid != $lastcid) {
// new category
// let's close the last open category (if any)
if ($lastcid)
printf ('</li></ul>');
// save current category
$lastcid = $r->cid;
// display category
printf ('<li>%s', $r->cslug, $r->cname);
// display first snippet
printf ('<li>%s</li>', $r->cslug, $r->sname, $r->sname);
} else {
// category already processed, just display snippet
// display snippet
printf ('<li>%s</a>', $r->cslug, $r->sname, $r->sname);
}
}
// let's close the last open category (if any)
if ($lastcid)
printf ('</li></ul>');
Note that I used printf but you should use your own function instead which wraps around printf, but runs htmlspecialchars thru the parameters (except the first of course).
Disclaimer: I do not necessarily encourage such use of <ul>s.
This code is just here to show the basic idea of processing hierarchical data got with one query.
First off, you shouldn't query your database in your view. That would be mixing your business logic and your presentation logic. Just assign the query results to a variable in your controller and iterate through it.
As for the query, yup a join can do that in 1 query.
SELECT * -- Make sure you only select the fields you want. Might need to use aliases to avoid conflict
FROM snippets S LEFT JOIN categiries C ON S.category = C.id
WHERE live = 1
ORDER BY S.category, C.name
This will get you an initial result set. But this won't give you the data nicely ordered like you expect. You'll need to use a bit of PHP to group it into some arrays that you can use in your loops.
Something along the lines of
$categories = array();
foreach ($results as $result) {
$snippet = array();
//assign all the snippet related data into this var
if (isset($categories[$result['snippets.category']])) {
$categories[$result['snippets.category']]['snippet'][] = $snippet;
} else {
$category = array();
//assign all the category related data into this var;
$categories[$result['snippets.category']]['snippet'] = array($snippet);
$categories[$result['snippets.category']]['category'] = $category;
}
}
This should give you an array of categories which have all the related snippets in an array. You can simply loop through this array to reproduce your list.
I'd try this one:
SELECT
c.slug,c.name,s.name
FROM
categories c
LEFT JOIN snippets s
ON s.category = c.id
WHERE live=1 ORDER BY c.name, s.name
I didnt test it, though. Also check the indexes using the EXPLAIN statement so MySQL doesnt do a full scan of the table.
With these results, you can loop the results in PHP and check when the category name changes, and build your output as you wish.
Besides a single combined query you can use two separate ones.
You have a basic tree-structure here with branch elements (categories table) and leaf elements (snippets table). The shortcoming of the single-query solution is that you get owner brach-element repeatedly for every single leaf element. This is redundant information and depending on the number of leafs and the amount of information you query from each branch element can produce large amount of additional traffic.
The two-query solution looks like:
$navQuery = $mysqli->query ("SELECT id, slug, name FROM categories WHERE live=1 ORDER BY name")
or die (mysqli_error ($mysqli));
$subNavQuery = $mysqli->query ("SELECT c.id AS cid, s.id, s.name FROM categories AS c LEFT JOIN snippets AS s ON s.category=c.id WHERE c.live=1 ORDER BY c.name, s.name")
or die (mysqli_error ($mysqli));
$sub = $subNavQuery->fetch_object (); // pre-reading one record
while ($nav = $navQuery->fetch_object ()) {
echo '<li>';
echo ''. $nav->name .'';
echo '<ul>';
while ($sub->cid == $nav->id) {
echo '<li>';
echo ''. $sub->name .'';
echo '</li>';
$sub = $subNavQuery->fetch_object ();
}
echo '</ul>';
}
It should print completely the same code as your example
$navQuery = $mysqli->query("SELECT t1.id AS cat_id,t1.slug,t1.name AS cat_name,t2.id,t2.name
FROM categories AS t1
LEFT JOIN snippets AS t2 ON t1.id = t2.category
WHERE t1.live=1
ORDER BY t1.name ASC, t2.name ASC") or die(mysqli_error($mysqli));
$current = false;
while($nav = $navQuery->fetch_object()) {
if ($current != $nav->cat_id) {
if ($current) echo '</ul>';
echo ''. $nav->cat_name .'<ul>';
$current = $nav->cat_id;
}
if ($nav->id) { //check for empty category
echo '<li>'. $nav->name .'</li>';
}
}
//last category
if ($current) echo '</ul>';

How do i GROUP BY or DISTINCT inside array?

I have searched high and low and cant find a similar issue to what i have.
I am a beginner so please forgive my clunky query structure.
I am trying to ( have attached screen grab below of output ):
Query the photos table to get the id based on category id and also start,limit because of pagination.
Query the photos tagged table based on the photo id i just got from the first query.
But my problem is that i cant group the tags, some photos have the same tag name. And the output just shows all the tags for each photo. I want restaurant to show only once etc...
<?php
// Get the file ideez and dont go beyond pagination start,limit eg:30,10
$queryFile = "SELECT id FROM $tableName WHERE cat_id=".$fileID." LIMIT $start, $limit";
$resultFile = mysql_query($queryFile);
while ($rowFile = mysql_fetch_array($resultFile)) {
// Get the tag names based on the file ideez retrived from the above query
$queryTagged = "SELECT tag_name FROM photoTagged WHERE file_id=".$rowFile['id']." GROUP BY tag_name";
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
while ($rowTagged = mysql_fetch_array($resultTagged)) {
$tagged = $rowTagged['tag_name'];
?>
<li><a href="#"><?php echo $tagged; ?></li>
<?php }} ?>
the above query is producing:
bar,cappucino,coffee,coffee machine,restaurant,bar,cappucino,coffee,coffee machine,restaurant,bar,coffee,restaurant,bar,coffee,coffee machine
restaurant,bar,cappucino,coffee,restaurant
what i need to show is:
bar,cappucino,coffee,coffee machine,restaurant
If anyone could help i would greatly appreciate it.
Thank you in advance.
John
My new code is
<?php
// Get the file ideez and dont go beyond pagination start,limit eg:30,10
$queryFile = "SELECT id FROM $tableName WHERE cat_id=".$fileID." LIMIT $start, $limit";
$resultFile = mysql_query($queryFile);
while ($rowFile = mysql_fetch_array($resultFile)) {
// Get the tag names based on the file ideez retrived from the above query
$queryTagged = "SELECT DISTINCT tag_name FROM photoTagged WHERE file_id=".$rowFile['id'];
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
$rowTagged = mysql_fetch_array($resultTagged);
$tagged = $rowTagged['tag_name'];
?>
<li><a href="#"><?php echo $tagged; ?></li>
<?php } ?>
I now get this: ( So i am close arent i? )
----------
cappucino
restaurant
bar
coffee machine
restaurant
coffee
coffee
restaurant
restaurant
restaurant
coffee
coffee
restaurant
restaurant
coffee machine
restaurant
coffee
I wonder if the spaces are something? i got that from copy and paste...
Any further help would be appreciated :-)
You should first perform a join between your photos and tags table, and THEN select the distinct tags.
I believe this query will let the database do all the work for you:
SELECT DISTINCT tag_name
FROM (SELECT file_id FROM $tableName WHERE cat_id=$fileID LIMIT $start, $limit) t1
LEFT JOIN photoTagged ON t1.id = photoTagged.file_id
You can also sort the tags in the database (ORDER BY tag_name).
Haven't tried it myself, so maybe the syntax is a bit off. But the idea should work.
distinct doesnt work if you are only getting one record at a time, so put the data in a PHP array and then use array_unique, which is PHPs way to do distinct
<?php
// Get the file ideez and dont go beyond pagination start,limit eg:30,10
$queryFile = "SELECT id FROM $tableName WHERE cat_id=".$fileID." LIMIT $start, $limit";
$resultFile = mysql_query($queryFile);
while ($rowFile = mysql_fetch_array($resultFile)) {
// Get the tag names based on the file ideez retrived from the above query
$queryTagged = "SELECT tag_name FROM photoTagged WHERE file_id=".$rowFile['id'];
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
$rowTagged = mysql_fetch_array($resultTagged)
$tagged[] = $rowTagged['tag_name'];
}
// Let PHP do the work.
$tagged=array_unique($tagged);
while (list(,$val) = each($tagged)) {
echo "<li><a href="#">$val</li>
}
?>
you need to do a sub-query to dodge the pagination problems with the photos. If you wish the selected tags to be a subset of the photos found in your first query, then you will need to do the following.
<?php
$queryTagged = "SELECT TAG.tag_name, count(TAG.tag_name) AS num FROM photoTagged as TAG JOIN (SELECT id FROM $tableName WHERE cat_id=$fileID LIMIT $start, $limit) as PHOTO ON (PHOTO.id = TAG.file_id) GROUP BY TAG.tag_name";
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
while ($tagged = mysql_fetch_assoc($resultTagged)) {
echo "<li id="'.$tagged['TAG.tag_name'].'"><a href="#">".$tagged['TAG.tag_name']." (".$tagged['TAG.num'].")</li>";
}
?>
This way you will have two queries, on for finding the photos, and one for finding the tags for the photos on that page. This technically takes a little longer as MySQL has to load the query into a temporary table, but it should work fine.
SELECT DISTINCT tag_name FROM photoTagged WHERE file_id=".$rowFile['id'] ?

Is it possible to echo multiple results outside of a while loop?

I have a while loop that I use to echo multiple mysql results that translate into offline users. To make things more organized, I wanted to perform the query, store the results in a variable and then echo it at the bottom of the page. I need to echo both online and offline users within a certain parent div, so it seems cluttered to me to echo the first div tag and then perform both queries and echo the results, then echo the closing tag. Currently, if I try to echo the result from outside the while loop, I only get 1 result. If anyone has any ideas, I would appreciate it.
$sql = 'SELECT * FROM users
LEFT JOIN friendships
ON friendships.friend_id = users.id
WHERE friendships.user_id = ?
AND users.id NOT IN (
SELECT active_users.id FROM active_users)';
$stmt5 = $conn->prepare($sql);
$result=$stmt5->execute(array($userid));
while ($row = $stmt5->fetch(PDO::FETCH_ASSOC)) {
$online=htmlspecialchars( $row['username'], ENT_NOQUOTES, 'UTF-8' );
$online = "<div class='user-online'>
<a data-name=\"$to\">$to</a>
</div>";
$online.=etc...plus do other processes
}
<div id=\"online\">
".$online."
</div>
You need to store your SQL results in an array
$sql = 'SELECT * FROM users
LEFT JOIN friendships
ON friendships.friend_id = users.id
WHERE friendships.user_id = ?
AND users.id NOT IN (
SELECT active_users.id FROM active_users)';
$stmt5 = $conn->prepare($sql);
$result=$stmt5->execute(array($userid));
while ($row = $stmt5->fetch(PDO::FETCH_ASSOC)) {
$online[]=htmlspecialchars( $row['username'], ENT_NOQUOTES, 'UTF-8' );
}
<div id=\"online\">
<?php foreach ($online as $aline) {
echo $aline;
} ?>
</div>
I've trimmed some code for clarity but hopefully it helps
Each iteration of the while loop is redefining the $online variable (overwriting it's previous value). You may either define one variable outside of the loop, and append to it in the loop, or store the results in an array, and loop through it to generate the output.
You don't need the while loop. Provided you have enough memory:
$rows = $stmt5->fetchAll(PDO::FETCH_ASSOC);

Categories