I have an app where I need to display today, and the next four days on.
$daysOn = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Mon'];
$daysOff = [ 'Sat', 'Sun' ];
$today = date("N", strtotime('today'));
$yesterday = date("N", strtotime('yesterday'));
$daysOnRearranged = array_merge(array_slice($daysOn, $today), array_slice($daysOn, 0, $yesterday));
This is currently showing me:
Monday, Monday, Tuesday, Wednesday, Thursday.
I need to show today, and the next for days on.
Any ideas?
Here I am using the strtotime ability to workout the date using a string like "Today + 1 day". I am also using the character "D" that returns 'A textual representation of a day, three letters'. You can change the "D" to a "l" if you want the full name of the day.
$nextDays = [];
for ($i = 0; $i <= 4; $i++) {
$nextDays[] = date("D", strtotime("today + $i day"));
}
var_dump($nextDays);
To remove weekends:
$nextDays = [];
$daysOff = ["Sat", "Sun"];
$n = 0;
do {
$day = date("D", strtotime("today + $n day"));
if (!in_array($day, $daysOff)) { // Check if the above $day is _not_ in our days off
$nextDays[] = $day;
}
$n ++; // n is just a counter
} while (count($nextDays) <= 4); // When we have four days, exit.
var_dump($nextDays);
On days check condition to be checked inside for loop.
Please have a look into below snippet and running code.
<?php
function pr($arr = [])
{
echo "<pre>";
print_r($arr);
echo "</pre>";
}
$daysOn = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Mon'];
$daysOff = ['Sat', 'Sun'];
$today = date("N", strtotime('today'));
$nextDays = [];
$j = $i = 0;
while($j != 4){ // until it found next 4 working days
$day = date('D', strtotime("today +$i day")); // it will keep checking for next working days
if(in_array($day, $daysOn)){ // will check if incremental day is in on days or not
$nextDays[] = $day; // it will add on days in result array
$j++;
}
$i++;
}
pr($nextDays);die;
Here is working demo.
EDIT
Above snippet is for checking on days.
You can check for OFF Days like below.
while($j != 4){
$day = date('D', strtotime("today +$i day"));
if(!in_array($day, $daysOff)){
$nextDays[] = $day;
$j++;
}
$i++;
}
pr($nextDays);die;
Related
I am stuck in this code, the problem is that I am trying to insert values inside the while loop and For loop, I have 584 Records and i need to insert 15 records into another table
<?PHP
$payroll_date = '2020-01-15';
$date_from = '2019-12-21';
$date_to = '2020-01-05';
$query = "SELECT * FROM adb_crosschex GROUP BY userid";
$result = mysqli_query($db, $query);
if ($result)
{
$row = mysqli_num_rows($result);
/* ######################################################################### */
$x = 0;
$sqltoDTR = "SELECT * FROM adb_crosschex GROUP BY userid";
$dtrResult = mysqli_query($db, $sqltoDTR);
while($rows = mysqli_fetch_array($dtrResult))
{
$x++;
$begin = new DateTime($date_from);
$end = new DateTime($date_to);
$pay_period = date("m-d", strtotime($date_from))." - ".date("m-d", strtotime($date_to));
$idcode = $rows['idcode'];
$controlno = $rows['userid'];
$acctname = $rows['acctname'];
$transdate = $rows['transdate'];
$branch = $rows['branch'];
for($i = $begin; $i <= $end; $i->modify('+1 day')) {
$date_trans = $i->format("Y-m-d");
/* result
2019-12-21
2019-12-22
2019-12-23
2019-12-24
....... until reaching the $end
*/
/* mysql Insert goes here */
}
}
/* ######################################################################### */
mysqli_free_result($result);
}
mysqli_close($db);
?>
the problem with the code is that it inserted data like forever i tried to leave it for 30 minutes and i get like 1 Million plus into my table hahahaha I supposed to get 584 x 15 = 8760 records Please Help
Ok i think i solve it for the code
for($i = $begin; $i <= $end; $i->modify('+1 day')) {
$date_trans = $i->format("Y-m-d");
myQuery($idcode, $payroll_date, $pay_period, $branch, $controlno, $date_trans, $acctname);
}
i try to put the insert query outside the loop and it goes like this
function myQuery($idcode, $payroll_date, $pay_period, $branch, $controlno, $date_trans, $acctname)
{
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$sql = "INSERT INTO `tbl_dtr`(idcode,payroll_date,pay_period,branch,controlno,date_trans,acctname)VALUES('$idcode', '$payroll_date', '$pay_period', '$branch', '$controlno', '$date_trans','$acctname')";
if ($db->query($sql) === TRUE) { echo "-<br>"; } else { echo $db->error; }
}
I am currently working on a project (A music website) with PHP Codeigniter. I want to fetch top 15 songs by number of downloads. To my knowledge, I got all unique ID's out ranging from highest to least. But now, I want to use each of these unique ID's to fetch data from another table in the database.
I have tried using for loop; but it seems to only return one of the rows which happens to be the first and highest number of occurrence in the array. The code below echo's the ID's but how can I use each of those ID's to fetch data.
function top15(){
$this->db->select('musiccode');
$result=$this->db->get('downloads');
$query = $result->result_array();
$downloads = array();
if(!empty($query)) {
foreach($query as $row)
{
$musiccode[] = $row['musiccode'];
}
$mode = array_count_values($musiccode);
arsort($mode);
$i = 0;
foreach ($mode as $field => $number) {
$i++;
echo $field." occured ". $number." times <br>";
if ($i == 15) {
break;
}
}
}
}
for ($b=0; $b < count($field); $b++) {
$this->db->where(array('track_musiccode'=> $field));
$field = $this->db->get('tracks');
return $field->result_array();
}
The code above only produce one row for me. I expect to have more than one and according to the number of ID's in the array. Thanks in advance.
For a single column condition, you could use where_in() instead and drop the for loop :
$this->db->where_in('track_musiccode', $field);
$field = $this->db->get('tracks');
return $field->result_array();
Yes Finally i did it. Incase if anyone needs it. this works just fine
function top15(){
$this->db->where('d_month', date('m'));
$this->db->select('musiccode');
$result=$this->db->get('downloads');
$query = $result->result_array();
$downloads = array();
if(!empty($query)) {
foreach($query as $row){
$musiccode[] = $row['musiccode'];
}
$res='';
$count=array_count_values($musiccode);
arsort($count);
$keys=array_keys($count);
for ($i=0; $i < count($keys); $i++) {
$res[] .= "$keys[$i]";
}
$this->db->where_in('track_musiccode', $res);
return $this->db->get('tracks')->result_array();
}
}
And I got this too. If I wasn't going to count occurrences but just iterate the number of downloads which is far more better than counting in other not to over populate the database and make your website work slowly.
function top15(){
$tracks = '';
$this->db->group_by('counter');
$result=$this->db->get('downloads');
$query = $result->result_array();
$counter = '';
$fields = '';
if(!empty($query)) {
foreach($query as $row) {
$counter[] = $row['counter'];
}
rsort($counter);
for ($i=0; $i<=15; $i++) {
$this->db->where('downloads.counter', $counter[$i]);
$this->db->join('tracks', 'tracks.musicid = downloads.musicid');
$fields[] = $this->db->get('downloads')->row_array();
}
return $fields;
}
}
Enjoy...
Here is my code:
$day = 1;
$dayList = array("0"=>"Sunday","1"=>"Monday");
if(in_array($day,$dayList)) {
echo $dayList[$day];
}
I tried $day = 0, and it works good, but it doesn't work if string is 1.
How can I solve this problem?
in_array() checks values, not keys. So in this instance it won't do what you want. isset() is the function you need to use, try this:
if (isset($dayList[$day])) {
...
}
Use array_key_exists :
$day = 1;
$dayList = array("0"=>"Sunday","1"=>"Monday");
if(array_key_exists($day,$dayList)) {
echo $dayList[$day];
}
More info:
http://php.net/manual/en/function.array-key-exists.php
Another method:
You could create a function that returns this value or the key like so:
function getDayOfTheWeek($name)
{
$dayList = array("0"=>"Sunday","1"=>"Monday","2"=>"Tuesday","3" => "Wednesday");
// if a string make sure it's capitalized
if (preg_match('/[^A-Za-z]/', $name)) {
$name = ucwords(strtolower($name));
} else {
// if not a string flip the array and get name
$dayList = array_flip($dayList);
}
return $dayList[$name];
}
// print results for each call to function
var_dump(getDayOfTheWeek('0')); // returns Sunday
var_dump(getDayOfTheWeek('1')); // returns Monday
var_dump(getDayOfTheWeek('Monday')); //returns 1
var_dump(getDayOfTheWeek('Tuesday')); // returns 2
I finally succeeded in making an image carousel by using php.
The code below works, but there are no images in it.
Now I am trying to make this array to get data from a Mysql database.
Could you show me an example of how do I do it?
<?php
//MySQL database connection
$mysqli = new mysqli('localhost', 'root', '', 'webshop');
$SQL = "SELECT image FROM productlist LIMIT 5";
$result = $mysqli->query($SQL);
$array = array(
0 => "picture1.jpg",
1 => "picture2.jpg",
2 => "picture3.jpg",
3 => "picture4.jpg",
4 => "picture5.jpg",
5 => "kalle6.jpg",
);
//Goes to previous page when clicking on prev
$index = $_GET['start'];
if ($index > 0) {
echo ' prev ';
} else {
echo ' prev ';
}
//Display 3 images
$show_img = 3;
$num_img = 0;
for($i = $_GET['start']; $i<count($array) && $num_img < $show_img; $i++) {
$num_img++;
echo "<img src=".$array[$i]."/>\n";
}
for($i=$num_img; $i<$show_img;$i++) {
echo "<img src=".(count($array) - 1)."/>\n";
}
//Goes to next page when clicking on next
if ($index < count($array) - 1) {
echo ' next ';
} else {
echo ' next ';
}
?>
You should have a look at this: http://php.net/manual/en/mysqli-result.fetch-array.php
/* numeric array */
/* $row is your $array */
$row = $result->fetch_array(MYSQLI_NUM);
echo "<pre>";
print_r($row);
echo "<pre>";
This should help you get the result you want
I am trying to count the number of occurrences in an array of cart items in Magento.
There are several items in the array, all with a price field (either $0 and $10)
What I'm looking to do, is to display the count of those items that have a price of 0
I currently have:
$session = Mage::getSingleton('checkout/session');
$items_array = $session->getQuote()->getAllItems();
foreach($items_array as $item) {
if ($item->getPrice() == 0) {
echo 'Item is free';
}
else {
}
}
This simply outputs all the free items. Ideally, I'd like to display just the count of such items.
Could I use something like array_count_values, but limit it to only count those values that are 0?
You can do that with several ways, but having that code the most easy one will be:
$session = Mage::getSingleton('checkout/session');
$items_array = $session->getQuote()->getAllItems();
$freeItems = 0;
foreach($items_array as $item) {
if ($item->getPrice() == 0) {
$freeItems++;
}
}
echo "There are $freeItems free items";
$session = Mage::getSingleton('checkout/session');
$items_array = $session->getQuote()->getAllItems();
$free = 0;
$notfree = 0;
foreach($items_array as $item) {
if ($item->getPrice() == 0) {
echo 'Item is free';
$free++;
}
else {
$notfree++;
}
}
echo 'total free items = ' . $free;
echo 'total nonfree items = ' . $notfree;