I am getting array after loping
while($num_rows1 = mysql_fetch_array($data_query_details1))
Now I want to check if my specific user_id ( suppose here user_id=3) stay 2/3 ( my specific value) times same date in array then print ok otherwise print need more.
Array
(
[0] => 3
[user_id] => 3
[1] => 2014-07-18
[date(FROM_UNIXTIME(date))] => 2014-07-18
)
Array
(
[0] => 3
[user_id] => 3
[1] => 2014-07-18
[date(FROM_UNIXTIME(date))] => 2014-07-18
)
Array
(
[0] => 4
[user_id] => 4
[1] => 2014-07-18
[date(FROM_UNIXTIME(date))] => 2014-07-18
)
any logic ?
$dates = array();
$stay = false;
$userid_to_check = 3;
while($row = mysql_fetch_array($data_query_details1)){
if($row['user_id']==$userid_to_check){
if(in_array($row[1], $dates)){
$stay = true;
break;
}
$dates[] = $row[1];
}
}
if($stay)echo 'ok';
else echo 'need more';
It's better to go with a more direct database select like the answers above though.
For example, what codebird mentioned:
$query = "select count(*) from your_table GROUP BY user_id HAVING date(FROM_UNIXTIME(date))='2014-07-18'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count>=2)echo 'ok';
else echo 'need more';
This should solve your issue:
SELECT user_id, COUNT(*) FROM your_table
WHERE DATE(FROM_UNIXTIME(date))='2014-07-18'
GROUP BY user_id
Related
I have 2 groups of banner ads and 2 banner ads. The database is like this.
groups_ads
| id | name | max_places
1 group1 3
2 group2 2
banner_ads
| id | group_id | url_images
1 1 https://example.com/banner.png
2 1 https://example.com/images.png
this code is to show all the banners_ads based on their group.
$res_arr = array();
$stmt = $conn->prepare("SELECT * FROM groups_ads");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while ($res = $stmt->fetch())
{
$result = array();
$max_slot = $res['max_places'];
$count = 0;
$banners = array();
$stmts = $conn->prepare('
SELECT * FROM banner_ads
WHERE group_id = "'.$res['id'].'"
');
$stmts->execute();
$stmts->setFetchMode(PDO::FETCH_ASSOC);
while ($count < $max_slot && $banner = $stmts->fetch()){
$count++;
if($banner){
$banners = array(
"type" => 2,
"url" => $banner['url_images'],
"count" => $count
);
}else{
$banners = array(
"type" => "default",
"count" => $count
);
}
$result[$count] = $banners;
}
$res_arr[$res['id']] = $result;
}
the result of this code is this
Array
(
[1] => Array
(
[1] => Array
(
[type] => 2
[url] => https://example.com/banner.png
[count] => 1
)
[2] => Array
(
[type] => 2
[url] => https://example.com/images.png
[count] => 2
)
)
[2] => Array
(
)
)
Because I have 2 banner ads in my database which is in group 1.
The question is, how to fill the empty row based on max_places ?
in database groups_ads, max_places for group 1 is 3. But in database banner_ads only have 2 banner. So how can I fill the empty places to my default string? so the result will be like this.
Array
(
[1] => Array
(
[1] => Array
(
[type] => 2
[url] => https://example.com/banner.png
[count] => 1
)
[2] => Array
(
[type] => 2
[url] => https://example.com/images.png
[count] => 2
)
[3] => Array
(
[type] => default
[count] => 3
)
)
[2] => Array
(
)
)
Thanks for help.
If you remove your test on $banner in the while loop conditional, you will ensure that you always fill $max_slots values for each array. Replace:
while ($count < $max_slot && $banner = $stmts->fetch()){
$count++;
if($banner){
with
while ($count < $max_slot) {
$count++;
if ($banner = $stmts->fetch()) {
I'm writing some data off my MySQL database into an array using columns with php like this:
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$row->id = $row["ID"];
$row->product = $row["product"];
$row->aantal = $row["Aantal"];
$row->price = $row["Price"];
$od[] = $row;
}
} else {
echo "0 results";
}
now later I have to use the $row->id and $row->product of the different columns seperatly to get some more data out of another table in MySQL. I've been trying to accomplish this:
$to = 0;
foreach($od as $odt)
{
$odb= $odt[$to]["ID"];
$sql = "SELECT `Name` FROM `detail` WHERE `ID` = '$odb'";
$to++;
But this doesn't seem to work, I've tried dozen of others but can't seem to get this thing right...
Any solutions or remarks?
EDIT:
Array ( [0] => Array ( [ID] => 3 [product] => 10 [Aantal] => 1 [Price] => 3 ) [1] => Array ( [ID] => 4 [product] => 13 [Aantal] => 1 [Price] => 3 ) [2] => Array ( [ID] => 5 [product] => 3 [Aantal] => 3 [Price] => 4 ) )
You are doing it wrong, you are saving object in an array and trying the get the data with array, it should be as
foreach($od as $key=>$odt)
{
$odb= $odt->id;
$sql = "SELECT `Name` FROM `detail` WHERE `ID` = '$odb'";
}
I have 3 tables users(id,name,etc..), trips(id,user_id,from,to,destionation) , vactions(id,user_id,from,to,destionation).
I use mysqli and i can not figure out how to do this
I need to fetch this table like this
Array
(
[0] => Array
(
[id] => 1
[current_status] => 0
[username] => user1
[fullname] => Eric Norman
[trips] = > Array
(
[0] => Array
(
[date_from] = 02/06/14
[date_to] = 05/06/14
[destination] = "Grece"
)
[vacations] = >
)
[1] => Array
(
[id] => 2
[current_status] => 0
[username] => user2
[fullname] => Joe Grey
[trips] = > Array
(
[0] => Array
(
[date_from] = 02/06/14
[date_to] = 05/06/14
[destination] = "Grece"
)
[vacations] = >
)
)
I've tried with left join but i doesn't work and my code now is only for users :
conn = new mysqli($host,$user,$password,$db);
$result = mysqli_query($conn, "SELECT id, current_status, username, CONCAT(first_name,' ',last_name) as fullname FROM user");
while ($row = $result->fetch_assoc()) {
$users[] = $row;
}
I think the best way is to get all users, then use a foreach to get all trips and vacations.
Because I'm not sure you can obtain this kind of array result using a simple SQL query, I think ou need an ORM.
$users = getAllUsers();
foreach($users as $user){
$user['trips'] = getTripsByUserId($user['id']);
$user['vacations'] = getVacationsByUserId($user['id']);
}
Of course you need to code the 3 methods "getAllUsers" ; "getTripsByUserId" and "getVacationsByUserId" which are only simple SELECT query to database with a where clause.
I have this query for mysql:
SELECT HOUR(time),COUNT(*) FROM pageview WHERE time >= DATE_SUB(NOW(),INTERVAL 12 HOUR) GROUP BY HOUR(time)
For example, this is the output:
Array
(
[0] => Array
(
[HOUR(time)] => 1
[COUNT(*)] => 1
)
[1] => Array
(
[HOUR(time)] => 10
[COUNT(*)] => 4
)
[2] => Array
(
[HOUR(time)] => 11
[COUNT(*)] => 5
)
)
However I want the output like this
Array
(
[1] => 1
[10] => 4
[11] => 5
)
The array index should be the value of [HOUR(time)].
I would prefer directly by changing the query.
To fetch the data I use this:
$stmt = $db->prepare($query);
$result = $stmt->execute();
$views = $stmt->fetchAll();
indexes in mysql results create automatically , to achieve your goal loop through your first array and create new one as you want
$result = array();
foreach($views as $row) {
$result[$row['HOUR(time)']] = array( 'COUNT(*)' => $row['COUNT(*)']);
}
print_r($result); // check output
or simpler form
$result = array();
foreach($views as $row) {
$result[$row['HOUR(time)']] = $row['COUNT(*)'];
}
print_r($result); // check output
Try with this
$res = array();
foreach($views as $data)
{
$res[$data['HOUR(time)']] = array( 'COUNT(*)' => $data['COUNT(*)']);
}
print_r($res);
I'm retrieving some data from a database where the values might be something like the following:
Column1 | Column2
-----------------
Bob | 24
Joe | 17
Jimmy | 38
Joe | 10
Bob | 5
Sam | 8
With this data, I want to somehow select the Bob rows and multiply them together. I want to do this for all rows that match in Column1. I'm not concerned about doing it in SQLite, but what is the best way to go about this in PHP? I've currently added the results to an array in PHP by doing the following:
$myArray[$resultRow['Column1']] = $resultRow['Column2'];
I was wondering how to go about navigating through this and finding matching keys, multipying their values together, and outputting something like
Column1 = Column2-a * Column2-b
consider this code:
$results = array();
foreach(array('Bob' => 24, 'Joe' => 17, 'Jimmy' => 38, 'Joe' => 10, 'Bob' => 5, 'Sam' => 8) as $key => $val){
$results[$key][] = $val;
}
echo '<pre>';
print_r($results);
echo '</pre>';
out put:
Array
(
[Bob] => Array
(
[0] => 5
)
[Joe] => Array
(
[0] => 10
)
[Jimmy] => Array
(
[0] => 38
)
[Sam] => Array
(
[0] => 8
)
)
PHP ignores duplicate key, you have to create $results array when you retreiving the data from database, lets assume that you do like this:
while () {
$col1 = $row["col1"];
$col2 = $row["col2"];
$results[$col1][] = $row["col2"];
}
now the results array must be like this:
Array
(
[Bob] => Array
(
[0] => 24
[1] => 5
)
[Joe] => Array
(
[0] => 17
[1] => 10
)
[Jimmy] => Array
(
[0] => 38
)
[Sam] => Array
(
[0] => 8
)
)
now, its easy to search for colmun1 names like this:
$values_for_colmn1 = $results[$column1]; //which is an array
// use array_product() function for array product
$product_for_column1 = array_product($values_for_colmn1);
First, to read only Bob's values:
$stmt = $db->prepare('SELECT Column2 FROM MyTable WHERE Column1 = ?')
$stmt->bindValue(1, 'Bob', SQLITE3_TEXT);
$result = $stmt->execute();
Then, multiply all of them:
$value = 1.0;
while ($res = $result->fetchArray()) {
$value = $value * $res['Column2'];
}