hello i have data array
function help_upd2($id)
{
$sql = $this->db->query("SELECT * FROM outbound WHERE id_outbound_voice='$id' ")->result();
$data = array();
foreach ($sql as $value) {
$data[] = $value->qty_outbound;
}
echo "<pre>", print_r($data);
// $a = $data * 100000;
// $this->db->query("UPDATE outbound SET total = '$a' WHERE id_outbound_voice = '$id'");
}
$data output ->
Array
(
[0] => 5 // * 100000
[1] => 1 // * 100000
[2] => 1 // * 100000
)
here I want to update the output value * 100000
When you are fetching from db it pulls them as string so you need to cast int on them to make math operations. Do this in the loop $data[] = (int) $value->qty_outbound * 100000;
Related
Background: I have an array created by:
$order = $_POST['order'];
The contents only have a single value per row:
[0] => 1
[1] => 4
[2] => 5
[3] => 2
[4] => 2
I am updating a database with this:
for ($i=0; $i<count($order); $i++)
{
$sql = "UPDATE catalog SET quantity=quantity - $order[$i] WHERE item_number= $i +1";
$result = mysqli_query($connection,$sql) or
die("<b>Query Failed.</b><br>" . mysqli_error($connection));
}
But I would like to use SELECT to search for records in which I have updated. Of course the problem is I do not have a direct item number to reference since I used $i +1 to refer to it.
As you can see the (array)key+1 to each value is equal to the item_number. So I was hoping to create an array in which the following is true:
for ($i=0; $i<count($order); $i++)
{
if ($order[$i] > 0)
{
$itemNum[$i] = 1;
} else {
$itemNum[$i] = 0;
}
I am sure I am over thinking this but I just need to be able to set up a WHERE statement displaying only item_num records which were just updated.
Make an array containing all the item_numbers you are updating. Then, select those item_numbers.
$updated = [];
for ($i=0; $i<count($order); $i++)
{
if ($order[$i] > 0) {
$updated[] = $i + 1;
$sql = "UPDATE catalog SET quantity=quantity - $order[$i] WHERE item_number= $i +1";
$result = mysqli_query($connection,$sql) or
die("<b>Query Failed.</b><br>" . mysqli_error($connection));
}
}
$sql = "SELECT * FROM catalog WHERE item_number IN (" . implode(",", $updated) . ")";
What are you going to do if the order is for more quantity than are remaining in catalog?
I need help about combine multiple array from database and fetch with
fetchAll(PDO::FETCH_ASSOC)
first I look for data member with percentage of 90% and every data in search in database in subtract one column in query "SELECT", for example like this :
$query1 = "SELECT * FROM tb_member WHERE category = '1' AND gender = 'M' AND age = '30'";
$stmt1 = $this->db->prepare($query1);
$stmt1->execute();
$data1 = $stmt1->fetchAll(PDO::FETCH_ASSOC);
$query2 = "SELECT * FROM tb_member WHERE category = '1' AND age = '30'";
$stmt2 = $this->db->prepare($query2);
$stmt2->execute();
$data2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
$query3 = "SELECT * FROM tb_member WHERE category = '1' AND gender = 'M'";
$stmt3 = $this->db->prepare($query3);
$stmt3->execute();
$data3 = $stmt3->fetchAll(PDO::FETCH_ASSOC);
And I combine the array with a sample code like this
$data = $data1 + $data2 + $data3;
I have tried and succeeded, but when one of the array is empty or unread it will error or not display any data even notification error
And i try make "if and else" like this
if(empty($data1)){
$data = $data2 + $data3;
}
elseif(empty($data2)){
$data = $data1 + $data3;
}
elseif(empty($data3)){
$data = $data1 + $data2;
}
else{
$data = $data1 + $data2 + $data3;
}
And page not show anything or blank, is there any other solution to solve this problem ?
UPDATE
After all i use array_merge() to combine multiple array from #Saral and when some variable is empty i use code sample like this
if(empty($array)){
$array = array();
}
And then it's work, thank you.
You combine array like this
$data = array_merge($data1, $data2, $data3);
For example,
$array1 = [];
$array2 = ['a', 'b'];
$array3 = ['l', 'k', 'm'];
$array4 = [];
$array = array_merge($array1, $array2, $array3, $array4);
print_r($array);
Output:
Array ( [0] => a [1] => b [2] => l [3] => k [4] => m )
It's weird that $array = $array1+$array2+$array3+$array4; gave me Array ( [0] => a [1] => b [2] => m )
The best way to combine array like this:
$data=compact($data1,$data2,$data3);
or
$data=array_combine($data1,$data2,$data3);
Hi guys i would like to ask help on my problem..
I have 4 variables made from my query:
$sql1 = mysql_num_rows(mysql_query("SELECT * FROM tbl_reservation WHERE YEAR(date_added) = YEAR(NOW()) AND order_action = 'Online Transaction' "));
$sql2 = mysql_num_rows(mysql_query("SELECT * FROM tbl_reservation WHERE YEAR(date_added) = YEAR(NOW()) AND order_action = 'Walkin Transaction' "));
$sql3 = mysql_num_rows(mysql_query("SELECT * FROM tbl_reservation WHERE YEAR(date_added) = YEAR(NOW())-1 AND order_action = 'Online Transaction' "));
$sql4 = mysql_num_rows(mysql_query("SELECT * FROM tbl_reservation WHERE YEAR(date_added) = YEAR(NOW())-1 AND order_action = 'Walkin Transaction' "));
I would like to put them in an array..to be exactly on my expectations.
Here is my sample code:
$stack = array($sql1, $sql2);
array_push($stack, $sql3, $sql4);
$query = print_r($stack);
$result = $query;
print json_encode($result);
It display like this on my browser:
Array ( [0] => 0 [1] => 8 [2] => 0 [3] => 1 ) true
But i want to display it like this:
[{"0","1"},
{"2","3"}]
I am planning to make a line graph on this.. I have a Chart.min.js and jquery.min.js
If any way.. The whole point.. i want to make a line graph that would compare my data from current year to last year.
Help me pls. :(
Give a try
<?php
$a = array(0,8,0,1) ;
echo "<pre>";print_r(json_encode(array_chunk($a, 2)));
?>
with that. You can get this result
[[0,8],[0,1]]
Btw, Why you need to do that with your array ? .
Edited
After i check your question again, So you want get the order
<?php
$stack = array(0,8,0,1) ;
$array2= array();
foreach ($stack as $key => $val) {
$array2[] = $key;
}
echo "<pre>";print_r(json_encode(array_chunk($array2, 2)));
?>
I have tables like this
listing_id
1
4
345
654
listing documents
listing_id folder filename
000345 full_menu_file testfile098
000345 header_menu_file testfile067
000004 full_menu_file testfile
000001 menu_file testfile567
000004 footer_menu_file testfile76
000004 test_menu_file testfile65
000345 footer_menu_file testfile764
000654 footer_menu_file testfile098
000654 footer_menu_file testfile078
Now I want to make an array and put those related data in that array. So the final output will be like this
array(
[1] => Array
(
[listing_id] => 1
[full_menu_file] =>
[header_menu_file] =>
[menu_file] =>
[footer_menu_file] =>
[test_menu_file] =>
)
[1] => Array
(
[listing_id] => 345
[full_menu_file] => testfile067
[header_menu_file] => testfile067
[menu_file] =>
[footer_menu_file] => testfile764
[test_menu_file] =>
)
[2] => Array
(
[listing_id] => 4
[full_menu_file] => testfile
[header_menu_file] =>
[menu_file] =>
[footer_menu_file] => testfile76
[test_menu_file] => testfile65
)
)
So I made my php code like this
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "listings";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$listing_query = "SELECT * FROM `listings` ORDER BY `listing_id` ASC ";
$cat_array = array();
if( $result = $mysqli->query($listing_query) ) {
while( $obj = $result->fetch_object() ) {
$listing_id = $obj->listing_id;
//Get all the file names
$get_images_name = "SELECT * FROM `listing_documents` WHERE `listing_id` = ".str_pad($listing_id, 6, '0', STR_PAD_LEFT)." ";
if( $img_query = $mysqli->query($get_images_name) ) {
while( $object = $img_query->fetch_object() ) {
if( $object->folder == 'full_menu_file' ) {
$full_menu_file_name = $object->filename;
}
if( $object->folder == 'header_menu_file' ) {
$header_menu_file_name = $object->filename;
}
if( $object->folder == 'menu_file' ) {
$menu_file_name = $object->filename;
}
if( $object->folder == 'footer_menu_file' ) {
$footer_menu_file_name = $object->filename;
}
}
$listing_array['listing_id'] = $listing_id;
$listing_array['full_menu_file_name'] = $object->full_menu_file_name;
$listing_array['header_menu_file'] = $object->header_menu_file;
$listing_array['menu_file'] = $object->menu_file;
$listing_array['footer_menu_file'] = $object->footer_menu_file;
array_push($cat_array, $listing_array);
}
}
print_r($cat_array);
But it is not showing the result as I need. So can someone tell me how to do this? Any suggestions will be really appreciable. Thanks
You need to put quotes around the result of str_pad so it will be treated as a string, not a number with leading zeroes.
$get_images_name = "SELECT * FROM `listing_documents` WHERE `listing_id` = '".str_pad($listing_id, 6, '0', STR_PAD_LEFT)."' ";
You could also combine both queries into a single JOIN:
SELECT l.listing_id as l_listing_id, ld.*
FROM listings as l
JOIN listing_documents AS ld ON ld.listing_id = LPAD(l.listing_id, 6, '0')
ORDER BY l_listing_id
The equivalent query without a join is:
SELECT *
FROM listing_documents
WHERE listing_id IN (SELECT LPAD(listing_id, 6, 0) FROM listings)
ORDER BY listing_id
I think your problem is the str_pad. You could use CONVERT or CAST instead:
SELECT * FROM `listing_documents` WHERE CAST(listing_id AS INT) = $listing_id
SELECT * FROM `listing_documents` WHERE CONVERT(INT, listing_id) = $listing_id
Your schema doesn't make sense to me. I don't know why you would use the padded string instead of an integer in your listing_documents table. But, using your current schema you could do:
$result = $mysqli->query(
<<<'QS'
SELECT listings.listing_id, listing_documents.folder, listing_documents.filename
FROM listings
JOIN listing_documents
ON listing_documents.listing_id LIKE LPAD(CONVERT(listings.listing_id,char),6,'0')
ORDER BY listings.listing_id ASC
QS;
I'm not actually sure if you need to convert the listing_id for lpad. And It probably is more performant to convert listing_documents.listing_id to an INT like ON CONVERT(listing_documents.listing_id,UNSIGNED INTEGER) = listings.listing_id
Then to add your results to an array:
$outputData = array();
while ($object=$result->fetch_object()){
if (!isset($outputData[$object->listing_id]))
$outputData[$object->listing_id] = array();
$outputData[$object->listing_id][$object->folder] = $object->filename;
}
With this approach, if a particular listing_id in listing_documents does not have all the different folders, then the key would be skipped.
Now, $outputData will have the data with array keys being the listing IDs.
To reset the array keys, you could do:
$outputData = array_values($outputData);
I have this function:
function findAllmessageSender(){
$all_from = mysql_query("SELECT DISTINCT `from_id` FROM chat");
$names = array();
while ($row = mysql_fetch_array($all_from)) {
$names[] = $row[0];
}
return($names);
}
that returns all the ID of my users in a private messaging system. Then I want to get the all the messages where the user_id is equal to the user logged in and from_id is equal to all from_id I got from the previous function:
function fetchAllMessages($user_id){
$from_id = array();
$from_id = findAllmessageSender();
$data = '\'' . implode('\', \'', $from_id) . '\'';
//if I echo out $ data I get these numbers '113', '141', '109', '111' and that's what I want
$q=array();
$q = mysql_query("SELECT * FROM chat WHERE `to_id` = '$user_id' AND `from_id` IN($data)") or die(mysql_error());
$try = mysql_fetch_assoc($q);
print_r($try);
}
print_r return only 1 result:
Array (
[id] => 3505
[from_id] => 111
[to_id] => 109
[message] => how are you?
[sent] => 1343109753
[recd] => 1
[system_message] => no
)
But there should be 4 messages.
You have to call mysql_fetch_assoc() for each row that is returned. If you just call mysql_fetch_assoc() once then its only going to return the first row.
Try something like this:
$result = mysql_query("SELECT * FROM chat WHERE `to_id` = '$user_id' AND `from_id` IN($data)") or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
'mysql_fetch_assoc' returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead.
You need to iterate array like:
while ($row = mysql_fetch_assoc($q)) {
echo $row["message"];
}