in Wordpress/WooCommerce MySQL database i have various shortcodes.
If there is 2 orders in the database then one of the orders is a shop_order and the next one is shop_subscription_order. We find those 2 datas in another tabel by calling post_id, which is named wp_posts. There we have to look on the shop_subscription_order if the order is wc_active. We need one mySQL call that looks on how many rows there is on each customer, if there is 2 rows it has to take post_id on the subscription_order and check if it is wc_active, and print all orders with that status.
A not working example but for show what i mean:
SELECT * from post_meta where meta_key=´_customer_id´ and meta_vlaue=´$customerid´ if number of rows='2' select * from wp_post where post_type='shop_supscription' whit the id form post_meta and post_status='wc-active'
and then echo num_of_rows
video example here:
Https:// nltrading.dk/video.mov
First in the video we show an order with 3 rows, and after we show an order with 2 rows.
We only need the orders with 2 rows, where there is an wc_active as shown in the video. We need to call all of this to count the number of rows where there is an wc_active whit only 2 rows of orders.
Use this like code:
//To show number of rows in table
function test()
{
global $wpdb;
$table_name = $wpdb->prefix . 'mydata';
$count_query = "select count(*) from $table_name";
$num = $wpdb->get_var($count_query);
echo $num . 'Rows Found';
}
Related
I have a query which returns posts from a database, the query groups the posts by category and the titles are links to the original post. The returned posts are displayed on a categories page.
The trouble is that whilst the post titles are displayed and grouped by category I find that only the first post_id for each post per category is found and only the first post or post with lowest ID in each category is returned by my link. The link brings you to the original post.
Original posts are displayed on a page called “entries.php”
Example:
Post1 id = 1
Post2 id = 2
Post3 id = 3
All the posts above are grouped by category but if I hover over them they all pickup Post1 id=1 for some reason. Is there something I can do to ensure that each id is found when I hover over them?
Thanks for your time!
Query:
$query = "SELECT category, GROUP_CONCAT(DISTINCT title SEPARATOR '<br />') titles, post_id
FROM categories, posts
WHERE categories.category_id = posts.category_id
GROUP BY category";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
while ($row = mysqli_fetch_array($result)){
echo "<h2>".$row['category']."</h2>";
echo ''.$row['titles'].'';
echo "<hr />";
}
You should GROUP BY post_id, also use ORDER BY yo sort as you whish.
You're missunderstanding SQL's concept of GROUPing. If you GROUP rows on category_id, what you actually are telling MySQL: I want exactly one row for each category_id, regardless of how many rows there exist. In standard SQL you are only allowed to "SELECT category_id" (and aggregate functions like COUNT, MAX, MIN and so on) when using "GROUP BY category_id". MySQL allows you a little more and arbitrarily selects one hit (the first it finds).
What you probably want is ORDER BY category_id, and then do the grouping in your php script like
$prev_cat_id = null;
while ($row = mysqli_fetch_array($result)) {
if ($prev_cat_id != $row['category_id']) {
do stuff for next category
} else {
do stuff for next post in same category
}
}
remember to do cleanup for last category
I have a query a Wordpress include which does the following:
$sql = "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'merchant_id' LIMIT 6";
$results = $wpdb->get_results($sql);
foreach ($results as $row)
{
echo $row->meta_value . ",";
//sprintf("SELECT * FROM retailers WHERE advertiserId = '%s'", $row->meta_value);
}
//clean all results
$wpdb->flush();
It parses all pages custom fields (merchant ID numbers), and returns any unique distinct values, separated by a comma. This bit works great.
ie: The above may return: 1301,345,723,134,1435
What I also have is a separate MySQL table of about 20 fields, three of which are the MerchantID, programmeName, and commissionMax. Each value from the CSV correlates with the MerchantID in the database.
Regardless of the amount of merchant ID's that appear in the CSV - I need to parse them all, but show the three highest commission rates (commissionMax), as well as the programmeName.
I managed to get connected to my external database and retrieve the appropriate values (using the commented code above) however this showed all of the retailers information.
Any advice?
Use the following query with limit:
SELECT * // select all fields
FROM table_name // from your table
WHERE MerchantID IN (your_ids_here) // IDs received from previous query or wherever
ORDER BY commissionMax DESC // descending sort by commissionMax field
LIMIT 3 // take first 3 results
How do i can echo the number of records saved in database. using php and my sql. for example i want to know the number of records uploaded on 28-August-2014..... where table name is product, and date as a field name. .
only echo the number of records, not to fetch all records.
Select count(1) as total from product where date='$date';
The mysql_num_rows function will spit out the number of rows returned by a query:
echo mysql_num_rows(mysql_query("SELECT id FROM product WHERE date BETWEEN '2014-08-28' AND '2014-08-28 23:59:59';"));
OR
$row = mysql_fetch_row(mysql_query("SELECT COUNT(1) FROM product WHERE date BETWEEN '2014-08-28' AND '2014-08-28 23:59:59';"));
echo $row;
I have created a query to select and count specific fields from one table and the corrisponding name from another table the query works when i run it in the sql on localhost but cant work out how to display the result to the page
e.g
Item name1 - count
tiem name2 - count
here is the query
$objects->connect();
// Count Query
$countQuery = 'SELECT a.`subId`, b.`subId`, b.`subTitle`, COUNT(a.`subId`) FROM `tbl_list`a, `tbl_subs`b WHERE a.`subId` = b.`subId` GROUP BY a.`subId`';
//Run the query
$objects->query($countQuery);
how would i display the result to the page
First your function $objects->query($countQuery); must return the result array which you can get like this:
$result = $objects->query($countQuery);
and then apply loop to $result;
Yes add COUNT(a.subId) as cnt as well and then like echo $result[0]->cnt;
depends upon your return array format.
How can I make a limit of showing the results? I need to limit it for 100 views.
In DB I have:
ID|NAME|PAGE|COUNT|DATE
In count I want to count untill 100 and then stop showing that ID. I could do it with count < 100. And then update the specific ID. I could get records with less than 100 views, but I couldn't manage to update count on the specific ID.
Row is showed with:
php code:
foreach($bannerGroups[0] as $ban) {
echo '<li class="right1">'.$ban->html().'</li>';
}
But I just don't know where to put the update in there. I tried, but all I got was to update only one ID. But it shows 4 on one page and randomizes them on refresh. So I don't know what to do.
Also I would like to say I am only learning php. Sorry for all the mess.
Code at http://pastebin.com/A9hJTPLE
If I understand correctly, you want to show all banners that have been previously-displayed less than 100 times?
If that's right, you can just add that to your WHERE clause:
$bannerResult = mysql_query("SELECT * FROM table WHERE page='cat' WHERE `COUNT` < 100");
To update them all, you can either run a query while displaying each individual banner, or "record" the id of each and run a single query at the end, like:
$ids = array();
foreach($bannerGroups[0] as $ban) {
$ids[] = $ban['ID']; // record the ID; don't know how Banner
// class works, assuming uses indexes; maybe ID() method?
echo '<li class="right1">'.$ban->html().'</li>';
}
...
mysql_query('UPDATE table SET `COUNT` = `COUNT` + 1 WHERE ID IN (' . join(',', $ids) . ')');
UPDATE:
Based off of a comment, your Banner class doesn't have a method to retrieve the individual banner's ID. In this case, you can record the ID values when you're building your banners array:
$ids = array();
while($row=mysql_fetch_assoc($bannerResult)) {
$banners[] = new Banner($row);
$ids[] = $row['ID']; // record the ID
}
// update the `count` on each record:
mysql_query('UPDATE table SET `COUNT` = `COUNT` + 1 WHERE ID IN (' . join(',', $ids) . ')');
sorry, but I got your question wrong...
first you have to insert a new sql-column like "viewcount" to the db...
on every read, you have to increment the value in viewcount...
for that behaviour (because, mysql does not allow sub-selects on update-clause on the same table), you have to fetch the results from db, as you do that, and pass all the primary-keys of the records to an array...
after the view-logic you have to fire up a query like:
UPDATE foo SET viewcount = viewcount + 1 WHERE id IN (1,2,3,4,5,6...,100);
where the IN-clause can be easily generated using your primary-keys-array with "implode(',', $arr);"
hope this helps.
$bannerResult = mysql_query("SELECT * FROM table WHERE page='cat' AND `count`<100");
#newfurniturey figured it out. in each foreach($banneruGroups added: $ids = $ban->getValue('id'); and then mysql_query("UPDATE dataa SET COUNT = COUNT + 1 WHERE id = '$ids'"); but is there any way to update them by adding query only once? And if the id is showed already 100 times i get Warning: Invalid argument supplied for foreach() in. Any idea how to fix it? I have 4 ids in DB . If one of them already have 100 views (count) then i get error!
Try to limit your data source for 100 items.
It's like OFFSET x LIMIT 100 in MySQL/PostgreSQL query or TOP 100 in MSSQL.