Im pulling data from 2 different WordPress tables and looping through to get the appropreate data into a table row.
$tid = $_POST['pqrnum']; //get estimate number
$results = $wpdb->get_results('SELECT * FROM eqnum WHERE id='.$tid); //getting estimate data
$pArray = explode(',', $results[0]->pnum); //creating array of product numbers in estimate
$ptable = "<table>"; //create table
$presults = $wpdb->get_results('SELECT * FROM eparts'); //getting product data
$pres= count($presults); //products array count
for($l = 0; $l <=count($pArray); $l++){ //loop through each product in the array
$item = $pArray[$l]; //getting item number start 0
$titem = $item + 1; //adding 1 to $item without disturbing it because product array starts at 1
for ($x = 1; $x <= $pres; $x++) { //looping through product array
if($titem == $presults[$x-1]->id){ // if estimate item = product number
$ptable .= "<tr><td>item num ". $titem ." presults id ".$presults[$x-1]->id." presults name ".$presults[$x-1]->name."</td></tr>";
}
}
}
$ptable .= "</table>";
not sure why, but it seems to be printing an extra row, in this particular example there should be 3 rows, however this is the response:
item num 1 presults id 1 presults name Back up Alarm
item num 10 presults id 10 presults name Oil Filter
item num 11 presults id 11 presults name Spark Plug
item num 1 presults id 1 presults name Back up Alarm
as you can see the first and last lines are the same, it should be just the top 3, not 4.
just trying to determine why the extra row, any help appreciated.
Alex had the answer using < rather than <=, I thought I had tried that before but prefect thank you.
Related
I have a database with multiple users. Each user has an id, name and position. I want to display users by position. To take an example I have a table with 7 numbered positions.
Position Name
1.
2.
3.
4.
5.
6.
7.
And 4 users in the database that I want to display according to their position.
Name Position
User_1 4
User_2 7
User_3 1
User_4 3
How can I do this without using the loop for or while for display?
I tried
for ($j=1; $j<=$no_rows; $j++){
$rand = mysqli_fetch_array($tabel);
$name[$j]=trim($rand['user']);
}
And then display for each number in that table that name[j].
Position Name
1. $name[1]
2. $name[2]
3. $name[3]
4. $name[4]
5. $name[5]
6. $name[6]
7. $name[7]
If those 4 users have positions 1,2,3,4, they are arranged by position, if one has the position higher than the total number of users then nothing is displayed, or it is displayed chaotically.
I also tried with j <= 7 but again the same problem.
The question needs more info but here is a rough idea based on what you have so far and my making some assumptions that this is a leader board style list.
// Something like: $query = 'SELECT * FROM users ORDER BY point DESC';
$count = 1;
while ($row = mysqli_fetch_array($query_results_object))
{
// Process data here.
echo $count . ': ' . $row['username'];
// Bail on 7.
if ($count >= 7)
{
break;
}
$count ++;
}
// Fill in empty values.
while ($count < 7)
{
echo $count . ': Space available.';
}
You did say without using the loop for display and your modified example appears to indicate you want rank instead of leader board position.
// Something like: $query = 'SELECT * FROM users';
$list = array_fill(1, 7, 'empty_value');
while ($row = mysqli_fetch_array($query_results_object))
{
// Process data here.
$list[$row['rank']] = $row['name'];
}
echo $list[1];
echo $list[2]];
...
This would be very inefficient as if you had 100 users you would be adding all of them to an in-memory table only to display 7 of them. So make sure you query does something like: SELECT TOP 7 * FROM users ORDER BY position DESC
I am trying to make a function where the following should happen:
Retrieve the ID value of all users in database.
Retrieve all numbers belonging to each user from an other table in the same database.
Add all the numbers belonging to each user together.
By using my code (see below), step 1 and 3 is working. At step 2, it is looping the correct amount of times but in every loop it retrieves the numbers belonging to the first ID from step 1.
Example:
Step 1 finds the following IDs: 1, 2, 3 and 4.
Step 2 loops 4 times, but retrieves the numbers belonging to ID 1 every time instead of retrieving the numbers to ID 1 in the first loop, ID 2 in the second etc.
My PHP:
$users_get = mysqli_query($conn,"SELECT id FROM users");
$users_num = mysqli_num_rows($users_get);
$users_list = array();
while($users_row = mysqli_fetch_array($users_get)){
$users_list[] = $users_row;
}
foreach($users_list as $users_row){
$users_items[] = array(
'id' => $users_row['id']
);
}
for($loop1 = 0; $loop1 < $users_num; $loop1++){
$numbers_get = mysqli_query($conn,"SELECT number FROM users_numbers WHERE userid = '".$users_items[$loop1]['id']."'");
$numbers_num = mysqli_num_rows($numbers_get);
$numbers_list = array();
while($numbers_row = mysqli_fetch_array($numbers_get)){
$numbers_list[] = $numbers_row;
}
foreach($numbers_list as $numbers_row){
$numbers_items[] = array(
'number' => $numbers_row['number']
);
}
$numbers_added = 0;
for($loop2 = 0; $loop2 < $numbers_num; $loop2++){
$numbers_added = $numbers_added + $numbers_items[$loop2]['number'];
}
}
I later added som echos to display the IDs and numbers that is retrieved and got the following result:
User ID: 1
Amount of numbers: 4
Numbers:
4 (Belonging to ID 1)
7 (Belonging to ID 1)
5 (Belonging to ID 1)
2 (Belonging to ID 1)
Total: 18
User ID: 2
Amount of numbers: 0
User ID: 3
Amount of numbers: 3
Numbers:
4 (Belonging to ID 1)
7 (Belonging to ID 1)
5 (Belonging to ID 1)
Total: 16
The amount of numbers for ID 3 is correct, however the 3 retrieved numbers belongs to ID 1.
Another observation I made was if I edit the SELECT query inside loop 1:
"SELECT number FROM users_numbers WHERE userid = '".$users_items[$loop1]['id']."'"
And manually selects an ID, example:
"SELECT number FROM users_numbers WHERE userid = '3'"
Then it retrieves the correct numbers belonging to ID 3.
After hours of trying to figure out what I'm doing wrong I still haven't found anything, so any help is really appreciated! Is there something I can do with my current code to fix it, or maybe there is some other ways to achieve the desired function?
Based on the comments on my question, I ended up with the following code:
$users_get = mysqli_query($conn,"SELECT a.id, SUM(b.number) AS number FROM users AS a INNER JOIN users_numbers AS b ON a.id = b.userid GROUP BY a.id ASC");
$loop1 = 0;
while($users_items[] = mysqli_fetch_array($users_get){
echo "ID: ".$users_items[$loop1]['id']." - Num total: ".$users_items[$loop1]['number']."<br />";
$loop1++;
}
The echo inside the while is only for testing purposes. When I run this, it displays a nice list with all the user IDs and the sum of each users numbers.
I'm totally a begginer trying to get this done. I get a list of ids from a query as 1,2,3,4... then I need to SUM the values of a column named "total" where the id includes one of the ids listed.
//Total quoted
//$array_quos = $row3['quo_list'];
$array_quos = explode(',', $row3['quo_list']);
foreach ($array_quos as $qtid => $qt)
{
$qt = $db->query("SELECT total FROM exp.quo_main WHERE quo_id = '.$qtid.' ");
$total_quoted2 += $qt;
}
In my project I am fetching data from the database using a while loop where each data is fetched by a customer id as $cid. Now all the data is calculated and finally put into a variable $cashflow. Now suppose there are 20 persons so the table will display 20 records where each data is counted for each person / customer. Now what I want to do is count the total of all 20 records and echo it seperately.
For eg.
Cust.1 : 500 (calculated from $cashflow)
Cust. 2 : 1000
Cust.3 : 800 ..... and so on.
Now these data are well echoed in my table. Now what I want is to add 500+1000+800 and echo 2300 as total separately. But I am not being able to figure out how to do it in my code. Should I take a variable in loop as a ++ counter? or something else? Please help.
Code:
$cash = "SELECT SUM(paid) FROM hbil WHERE cid = '".$cid."' AND method = 'cash'";
$cash_qur = mysql_query($cash) or die(mysql_error());
$cash_neg = "SELECT SUM(price) FROM hbil WHERE cid = '".$cid."' AND method = 'cash' AND price < 0";
$cash_qur_neg = mysql_query($cash_neg) or die(mysql_error());
while($cash_fetch = mysql_fetch_array($cash_qur))
{
$cash_fet = mysql_fetch_array($cash_qur_neg);
$cashpay = $cash_fetch['SUM(paid)'];
$cashneg = $cash_fet['SUM(price)'];
$cash_total = $cashpay-$cashneg;
$cashflow = number_format($cash_total,2,'.',',');
}
$total = 0;
while (...) {
//...
$total += $cash_total;
}
echo $total;
Will do the trick
I have "price offer" items under certain processes;
PROCESS 1
index no 0 price offer a count=1 ($selected_price_offers_cnt)
PROCESS 2
index no 1 price offer b count=3 (3+1=4)
index no 2 price offer c count=3 (3+1=4)
index no 3 price offer d count=3 (3+1=4) 3=selected_price_offers_cnt, 1=$previous, 4=$next
PROCESS 3
index no 4 price offer e count=2 (2+4=6)
index no 5 price offer f count=2 (2+4=6) 2=selected_price_offers_cnt, 4=$previous, 6=$next
What I need to do is to print "scale sub total" only below the LAST price offer item of EACH PROCESSES. In other words, below a, d and f.
In order to do that I try to compare the latest count of the price offers from the beginning (1, 4 and 6) with the current row index no + 1.
If we arrive at the last price offer of a PROCESS X, its count will be == index no + 1 (like at process offer d count 4 = index no 3 + 1).
So I need to keep $previous variable updated with $next for the following PROCESS Y.
My code takes unnaturally long time to finish execution and at last it prints only one sale sub total line.
(As this code is used inside Drupal views global php field, SQL queries are arranged to work there and they return correct values, no problem there.)
Any approaches other than while...loop is also welcomed.
<?php
$process_nid = $row->nid_3;
$current_index_no = $view->row_index; //the number of the row that is being processed
$selected_price_offers = db_query("SELECT entity_id FROM drupal_field_data_field_process WHERE bundle = 'price_offer' AND deleted = '0' AND field_process_nid = {$process_nid} AND entity_id IN (SELECT content_id FROM drupal_flag_counts WHERE fid='18' AND content_type='node' AND COUNT = '1')");
$selected_price_offers_cnt = db_query("SELECT COUNT(entity_id) FROM drupal_field_data_field_process WHERE bundle = 'price_offer' AND deleted = '0' AND field_process_nid = {$process_nid} AND entity_id IN (SELECT content_id FROM drupal_flag_counts WHERE fid='18' AND content_type='node' AND COUNT = '1')")->fetchField();
$previous = 0;
while($current_index_no) {
$next = $selected_price_offers_cnt + $previous; // add the previous to the current amount
if ($next == ($current_index_no+1)) {
$previous = $next; // save the new price offer count to the $previous in order to use it at the next row
// this part below works as expected
$sum1 = 0;
foreach($selected_price_offers as $item) {
$po_item= $item->entity_id;
$scale1 = db_query("SELECT field_sub_total_scale_1_value FROM drupal_field_data_field_sub_total_scale_1 WHERE bundle = 'price_offer' AND deleted = '0' AND entity_id = {$po_item}")->fetchField();
$sum1+= $scale1;
}
print "scale sub total: ".number_format($sum1,2,",","."). " TL";
}
else {
echo "";
}
}
?>