Getting Multiple Values from a table column into a repeating row - php

I'm using Wordpress and I have to figure out how to get multiple values out of the same column and turn them into variables.
The last step is I just want the data to display in a table like so:
Bob Company1 <br>
Alex Company2
Instead I get either Bob Alex Company1 Company 2 or
Bob <br>
Alex<br>
Company1<br>
Company2
Here's two different versions I'm working with:
$sql = "SELECT meta_value as guest from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guest_name' UNION SELECT meta_value as company from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guests_company'";
$result = mysql_query($sql);
$NumberOfResults=mysql_num_rows($result);
if (mysql_num_rows($result) == 0) {
echo "No Guests yet... stay tuned!";
exit;
}
while(list($guest,$company)= mysql_fetch_row($result))
{
echo "<table><tr><td>".$guest."</td><td>".$company."</td></tr></table>";
}
or
$sql = mysql_query("SELECT meta_value as guest from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guest_name' LIMIT 2 UNION SELECT meta_value as company from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guests_company'");
$i = 1;
while ($get = mysql_fetch_array($sql))
{
echo '<table><tr><td>'.$get["guest"].'</td><td>'.$get["company"].'</td></tr></table>';
$i++;
}
ANY HELP would be so appreciated!!! thanks!
Edit: In case anybody in the future wants to create their own widget using advanced custom fields in Wordpress, here's the final product more or less:
$sql = "SELECT p.ID AS post_id, g.meta_value as guest, c.meta_value as company, d.meta_value as date
FROM wp_posts p
JOIN wp_postmeta g ON g.post_id = p.id AND g.meta_key = 'guest_name'
JOIN wp_postmeta c ON c.post_id = p.id AND c.meta_key = 'guests_company'
JOIN wp_postmeta d ON d.post_id = p.id AND d.meta_key = 'show_date'
WHERE p.post_status = 'publish'
ORDER by d.meta_value DESC";
$query = mysql_query($sql);
echo '<table><tr><th>Guest</th><th>Company</th><th>Show Date</th></tr>';
while ($get = mysql_fetch_array($query)) {$newDate = date("m-d-Y", strtotime($get["date"]));
echo '<tr><td>'.$get["guest"].'</td><td>'.$get["company"].'</td><td>'.$newDate.'</td></tr>';
}
echo '</table>';
Really appreciated doublesharp's help on this.

If I understand your question correctly, you have to write the code to display table like this:
echo '<table>';
while ($get = mysql_fetch_array($sql))
{
echo '<tr><td>'.$get["guest"].'</td><td>'.$get["company"].'</td></tr>';
}
echo '</table>';

If the postmeta values are attached to different posts as indicated by the different 'post_type' values in your query, then you will need some other identifying information to link them together. If there is a single post_id that has both a guest_name and guest_company meta_value then you can join it twice to get the results. Using a UNION will always result in them coming back in different rows.
// Join the postmeta table to posts twice, once for each meta_key.
$sql = <<<SQL
SELECT p.ID AS post_id, g.meta_value as guest, c.meta_value as company
FROM wp_posts p
JOIN wp_postmeta g ON g.post_id = p.id AND g.meta_key = 'guest_name'
JOIN wp_postmeta c ON c.post_id = p.id AND c.meta_key = 'guests_company'
WHERE p.post_status = 'publish'
SQL;
// Execute the query
$query = mysql_query($sql);
// Start your table for output
echo '<table>';
while ($get = mysql_fetch_array($query)) {
// Write the output of each row
echo '<tr><td>'.$get["guest"].'</td><td>'.$get["company"].'</td></tr>';
}
// Close the table
echo '</table>';
If this is inside Wordpress, I would recommend using the $wpdb object for your query.

Related

How to make comlex query to Wordpress database

I tried to make a complex query to WordPress database but it did not work, so I ask for help.
First I need to get the meta value from one table, and then using it to get the ID from another table.
For this I use the following code:
global $wpdb;
$event_ID = 306;
$meta_value = $wpdb->get_var( "SELECT meta_value FROM wp_postmeta
WHERE meta_key = 'driver_type' AND post_id = '{$event_ID}'" );
$event_type = $wpdb->get_var( "SELECT post_title FROM wp_posts
WHERE ID = '{$meta_value}'" );
I would like to optimize the query. Tried to make here such request but it returns to me 1 instead of the necessary value:
$complex = $wpdb->query( "SELECT t2.post_title, t1.meta_value FROM wp_postmeta AS t1
INNER JOIN wp_posts AS t2 ON t1.meta_value = t2.ID
WHERE meta_key = 'driver_type' AND post_id = '{$event_ID}' GROUP BY t1.post_id" );
Please tell me how to make the correct complex query in my case.
You should be able to just chain together your two queries:
SELECT post_title
FROM wp_posts
WHERE ID = (SELECT meta_value FROM wp_postmeta
WHERE meta_key = 'driver_type' AND post_id = '{$event_ID}');
Or, you could try doing a join:
SELECT p.post_title
FROM wp_posts p
INNER JOIN wp_postmeta m
ON p.ID = m.meta_value
WHERE
m.meta_key = 'driver_type' AND post_id = '{$event_ID}';

Mysql Query Join Table on the most recent row

I have a page of categories, they worked fine until I did a join.
The categories display like so:
category 1-----------------------0----------0
discussion 1 by someone
category 2-----------------------0----------0
discussion 2 by someoneelse
Now where it says discussion, I need to display the last discussion posted to that category based on it's discussion_id. I have tried ORDER BY ... DESC but it sorts by category names not the discussion names and posted by.
$sql = "SELECT *, COUNT(d.cat_id) as count
FROM discussions as d
LEFT JOIN categories c ON (c.cat_id = d.cat_id)
RIGHT JOIN soldiers s ON (s.uid = d.discussion_poster)
GROUP BY d.cat_id";
$result = query($sql);
while (($row = mysqli_fetch_assoc($result)) != false) {
$cat_id = $row['cat_id'];
$discussion_id = $row['discussion_id'];
$cat_title = $row['cat_title'];
$discussion_title = $row['discussion_title'];
$discussion_time = $row['discussion_time'];
$count = $row['count'];
$discussion_poster_id = $row['discussion_poster'];
$discussion_poster = $row['soldier'];
}
$sql = "
SELECT *
FROM categories c
INNER JOIN
(
SELECT MAX(discussion_id) discussion_id,
COUNT(discussion_id) as count,
cat_id
FROM
discussions
GROUP BY
cat_id
) as d1
ON (c.cat_id = d1.cat_id)
INNER JOIN discussions as d
ON (d1.discussion_id = d.discussion_id)
INNER JOIN soldiers s
ON (s.uid = d.discussion_poster)
GROUP BY d.cat_id";

MySQL and PHP: Inner join and result

Here is a mysql request:
$requete1 = mysql_query("
SELECT a.post_id, e.meta_value
FROM wp_postmeta as b
INNER JOIN wp_postmeta AS a ON b.post_id = a.post_id
INNER JOIN wp_postmeta AS d ON b.post_id = d.post_id
INNER JOIN wp_postmeta AS e ON b.post_id = e.post_id
WHERE b.meta_key = '_show_hide_sales'
AND b.meta_value = '1'
AND d.meta_key = '_sold_sales'
AND d.meta_value != '3'
AND e.meta_key = '_sales_sort_number'
ORDER BY CAST(e.meta_value as UNSIGNED INTEGER) DESC
LIMIT ".($from-1).", ".($range+1)
);
while($resultat1 = mysql_fetch_array($requete1))
{
$res2= $resultat1['post_id']; // this one is ok
$res3= $resultat1['meta_value']; // this one is ambiguous
};
The search on the post_id ($res2) is ok as it is unambiguous.
But there are three possible meta_keys here:
The one that is related to "_show_hide_sales".
The one that is related to "_sold_sales".
The one that is related to "_sales_sort_number".
But how can I get the meta_value ($res3) that is related to the "_sales_sort_number" meta_key?
My SELECT clearly indicates that I want to retrieve the e.meta_value which should be associated to the "_sales_sort_number" meta_key but it doesn't seem to work.
Thank you for your guidance.

MySQL - Ignore WHERE condition if key/value pair doesn't exist

In a Wordpress order based system I am selecting people going to an event on a certain date and location.
I also need to see if there are any order notes existing for any matching orders, which I do by selecting the meta_value where the meta_key ="_wc_acof_2".
The problem is when I add the condition to my WHERE clause the code stops returning records where that meta key/value pair doesn't exist (i.e. there are no order notes). Whereas I still need to select those records just return null or similar for the order notes.
The problem line in the WHERE clause is:
AND jn_postmeta_guestnotes.meta_key = "_wc_acof_2"
In my head I would like to write that line similar to:
AND (jn_postmeta_guestnotes.meta_key = "_wc_acof_2" OR 'there is no matching meta_key so just ignore this condition and return the record anyway')
The full query:
SELECT
CONCAT_WS(" ",wp_postmeta.meta_value,jn_postmeta_lastname.meta_value) AS Name,
wp_woocommerce_order_itemmeta.meta_value AS Adults,
jn_postmeta_guestnotes.meta_value AS Notes
FROM
wp_postmeta
LEFT JOIN
wp_postmeta AS jn_postmeta_lastname ON wp_postmeta.post_id = jn_postmeta_lastname.post_id
LEFT JOIN
wp_postmeta AS jn_postmeta_guestnotes ON wp_postmeta.post_id = jn_postmeta_guestnotes.post_id
LEFT JOIN
wp_posts ON wp_postmeta.post_id = wp_posts.ID
LEFT JOIN
wp_woocommerce_order_items ON wp_woocommerce_order_items.order_id = wp_posts.ID
LEFT JOIN
wp_woocommerce_order_itemmeta ON wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
LEFT JOIN
wp_woocommerce_order_itemmeta AS jn_woocommerce_order_itemmeta_location ON wp_woocommerce_order_itemmeta.order_item_id = jn_woocommerce_order_itemmeta_location.order_item_id
LEFT JOIN
wp_woocommerce_order_itemmeta AS jn_woocommerce_order_itemmeta_date ON wp_woocommerce_order_itemmeta.order_item_id = jn_woocommerce_order_itemmeta_date.order_item_id
WHERE
1 = 1
AND wp_postmeta.meta_key = "_billing_first_name"
AND jn_postmeta_lastname.meta_key = "_billing_last_name"
AND jn_postmeta_guestnotes.meta_key = "_wc_acof_2"
AND wp_woocommerce_order_itemmeta.meta_key = "Adults"
AND jn_woocommerce_order_itemmeta_location.meta_key = "Booking Type"
AND jn_woocommerce_order_itemmeta_location.meta_value LIKE "%'.$showlocation.'%"
AND jn_woocommerce_order_itemmeta_date.meta_key = "Booking Date"
AND (jn_woocommerce_order_itemmeta_date.meta_value = "'.$showdateformata.'" OR jn_woocommerce_order_itemmeta_date.meta_value = "'.$showdateformatb.'")
AND wp_posts.post_status = "wc-completed"
GROUP BY wp_posts.ID
Any advice much appreciated!
The answer is to put the extra bit in a sub-select like so:
(SELECT wp_postmeta.meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = "_wc_acof_2" AND wp_postmeta.post_id = wp_posts.ID) AS Notes
Just put all filtering condition on the ON so the left join work. On the WHERE will remove the nulls
.....
wp_woocommerce_order_itemmeta AS jn_woocommerce_order_itemmeta_date
ON wp_woocommerce_order_itemmeta.order_item_id = jn_woocommerce_order_itemmeta_date.order_item_id
AND 1 = 1
AND wp_postmeta.meta_key = "_billing_first_name"
AND jn_postmeta_lastname.meta_key = "_billing_last_name"
AND jn_postmeta_guestnotes.meta_key = "_wc_acof_2"
AND wp_woocommerce_order_itemmeta.meta_key = "Adults"
AND jn_woocommerce_order_itemmeta_location.meta_key = "Booking Type"
AND jn_woocommerce_order_itemmeta_location.meta_value LIKE "%'.$showlocation.'%"
AND jn_woocommerce_order_itemmeta_date.meta_key = "Booking Date"
AND
(
jn_woocommerce_order_itemmeta_date.meta_value = "'.$showdateformata.'"
OR
jn_woocommerce_order_itemmeta_date.meta_value = "'.$showdateformatb.'"
)
AND wp_posts.post_status = "wc-completed"

how to remove WordPress tag on post

hi i want to make rest api to get the post from wordpress database . as the code below here
$query="select wp_postmeta.meta_value,wp_terms.*,wp_posts.* from wp_posts
left join wp_term_relationships ON wp_posts.id=wp_term_relationships.object_id
left join wp_term_taxonomy on wp_term_taxonomy.term_id=wp_term_relationships.object_id
left join wp_terms on wp_terms.term_id = wp_term_taxonomy.term_id
left join wp_postmeta on wp_postmeta.post_id=wp_posts.id
where 1=1 order by post_date desc
limit 40";
$query_run= mysql_query($query);
while ($row = mysql_fetch_assoc($query_run)) {
$query_image= mysql_query("select meta_value from wp_postmeta where meta_key='_wp_attached_file' and post_id='".$row['id']."'");
$row1= mysql_fetch_assoc($query_image);
$row['image']=$row1['meta_value'];
$detail[]=$row;
}
echo json_encode($detail);
The problem is that i got lot of html tag. i use stri_tags() function and preg_replace().
but i does not got any responsive json. is there any solution for that or any other api that is in build for this task plz help me
Thanks and Regards.

Categories