$now = time();
$day7 = array();
$day7_srt="";
//get last 7days
for($i=0; $i<7; $i++){
$d = mktime(0,0,0,date('m',$now),date('d',$now)-$i);
$day7[$i]["d"] = date('d',$d);
$day7[$i]["m"] = date('m',$d);
$day7[$i]["y"] = date('Y',$d);
$day7[$i]["date"] = date('Y-m-d',$d);
}
sort($day7);
foreach ($day7 as $key => $value) {
$d = $value['d'];
$m = $value['m'];
$day7_srt .= "\"$m-$d\",";
}
$day7_srt=substr("$day7_srt",0,-1);
echo $day7_srt;
I need get the last 7 days text.
if not sort($day7), I will get.
"12-02","12-01","11-30","11-29","11-28","11-27","11-26"
if use sort($day7)
"12-01","12-02","11-26","11-27","11-28","11-29","11-30"
But I need
"11-26","11-27","11-28","11-29","11-30","12-01","12-02"
<?php
$now = time();
$day7 = array();
$day7_srt="";
//get last 7days
for($i=0; $i<7; $i++){
$d = mktime(0,0,0,date('m',$now),date('d',$now)-$i);
$day7[$i]["d"] = date('d',$d);
$day7[$i]["m"] = date('m',$d);
$day7[$i]["y"] = date('Y',$d);
$day7[$i]["date"] = date('Y-m-d',$d);
}
asort($day7);
foreach ($day7 as $key => $value) {
$d = $value['d'];
$m = $value['m'];
$day7_srt .= "\"$m-$d\",";
}
$day7_srt=substr("$day7_srt",0,-1);
$array = explode(",",$day7_srt);
sort($array);
$day7_srt = implode(",",$array);
echo $day7_srt;
?>
o/p
"11-26","11-27","11-28","11-29","11-30","12-01","12-02"
Related
I have created a small script which gives me random numbers. Now I want to create a list of this random number list with repeating numbers.
It should generate match numbers of this list of random numbers (A match number is a number which repeats)
For instance 4 – 6 – 9 – 32 – 34 – 31 – 5 – 32 (The match number here is 8 because after 8 numbers we have a repeater). I would like all the match numbers to be echo beside each other with a space in between.
Can someone help me?
I have tried creating an if statement but I can't get it working.
for ($rnd=1;$rnd<=50;$rnd++)
{
$random = mt_rand(0,100) . " ";
echo $random;
}
Explanation coming soon.
$numbers = array();
for($i = 1; $i <= 50; $i++) {
$number = mt_rand(0,100);
if(!isset($numbers[$number])) $numbers[$number] = array();
$numbers[$number][] = $i;
}
foreach($numbers as $key => $value) {
$start = '';
foreach($value as $k => $v) {
echo $start . $key . ' (Match Number: ' . $v . ')';
$start = ' - ';
}
echo '<br />';
}
If I don't misunderstood your question then this is what you want. Let's do this way-
<?php
$existing = [];
$repeat_numbers = [];
for ($rnd=1;$rnd<=50;$rnd++)
{
$random = mt_rand(0,100);
if(in_array($random,$existing)){
$repeat_numbers[] = $rnd; // pushing the repeating index
}
$existing[] = $random;
echo $random.PHP_EOL;
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO: https://3v4l.org/eEhB8
AS PER THE COMMENT
<?php
$existing = [];
$repeat_numbers = [];
for ($rnd=1;$rnd<=50;$rnd++)
{
$randoms[] = mt_rand(0,100);
}
echo implode('-',$randoms).PHP_EOL;
$i = 1;
foreach($randoms as $rnd){
if(in_array($rnd,$existing)){
$repeat_numbers[] = $i;
$i=1;
}
$existing[] = $rnd;
$i++;
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO https://3v4l.org/Xjc5X
AS PER THE LATEST COMMENT
<?php
$existing = [];
$repeat_numbers = [];
$randoms = explode('-','3-31-34-29-28-5-28-23-31-4-1-31-11-17-23-9-20-24-22-3-11-24-26-4-10');
$i = 1;
foreach($randoms as $rnd){
if(in_array($rnd,$existing)){
$repeat_numbers[] = $i;
$i=1;
$existing = []; // This like will do the magic for you
}
$existing[] = $rnd;
$i++;
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO: https://3v4l.org/hIT22
FINAL EDIT:
<?php
$existing = [];
$repeat_numbers = [];
$randoms = explode('-','4-9-13-18-19-34-23-9-9-13-44-5-13-13-88-26-29-27-34-67-65-83-26');
$i = 1;
foreach($randoms as $rnd){
if(in_array($rnd,$existing)){
$repeat_numbers[] = $i;
$i=1;
$existing = [];
}else{
$existing[] = $rnd;
$i++;
}
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO: https://3v4l.org/9j7iq
EDITED AGAIN:
<?php
$existing = [];
$repeat_numbers = [];
$randoms = explode('-','4-9-13-18-19-34-23-9-9-13-44-5-13-13-88-26-29-27-34-67-65-83-26');
//print_r($randoms);
$i = 0;
foreach($randoms as $rnd){
$i++;
if(in_array($rnd,$existing)){
$repeat_numbers[] = $i;
if(count($existing)==1){
$i=1;
}else{
$i=0;
}
$existing = [];
}
$existing[] = $rnd;
//print_r($existing);
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO: https://3v4l.org/VfIJY
I'm working on my PHP to get the list of time. I'm using DomDocument to get the time, I want to find a way to reduce the code as I have got the 69 tags of time in my get-listing.php script.
if I use this:
$time1 = $xpath->query("*/span[#id='time1']");
$time2 = $xpath->query("*/span[#id='time2']");
$time3 = $xpath->query("*/span[#id='time3']");
$time4 = $xpath->query("*/span[#id='time4']");
$time5 = $xpath->query("*/span[#id='time5']");
$time6 = $xpath->query("*/span[#id='time6']");
$time7 = $xpath->query("*/span[#id='time7']");
...etc until to get time69
$time69 = $xpath->query("*/span[#id='time69']");
It will be too large for me to write the list of code to parsing the time from the time1 tag to time69 tag.
<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;
function getState($string)
{
$ex = explode(" ",$string);
return $ex[1];
}
$xml .= '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '
<tv generator-info-name="www.mysite.com/xmltv">';
$baseUrl = file_get_contents('http://www.mysite.com/get-listing.php');
$domdoc = new DOMDocument();
$domdoc->strictErrorChecking = false;
$domdoc->recover=true;
//#$domdoc->loadHTMLFile($baseUrl);
#$domdoc->loadHTML($baseUrl);
$links = $domdoc->getElementsByTagName('a');
$i = 0;
$count = 0;
$data = array();
foreach($links as $link)
{
//echo $domdoc->saveXML($link);
if($link->getAttribute('href'))
{
if(!$link->hasAttribute('id') || $link->getAttribute('id')!='streams')
{
$url = str_replace("rtmp://", "", $link->getAttribute('href'));
$url = str_replace(" ", "%20", $link->getAttribute('href'));
//echo $url;
//echo "<br>";
$sdoc = new DOMDocument();
$sdoc->strictErrorChecking = false;
$sdoc->recover=true;
#$sdoc->loadHTMLFile($url);
$time1_span = $sdoc->getElementById('time1');
//$spans = $sdoc->getElementsByTagName('time1');
$query = parse_url($url)['query'];
$channel_split = explode("&", $query)[0];
$channel = urldecode(explode("=",$channel_split)[1]);
$id_split = explode("&", $query)[1];
$my_id = urldecode(explode("=",$id_split)[1]);
$xpath = new DOMXpath($sdoc);
$time1 = $xpath->query("*/span[#id='time1']");
$time2 = $xpath->query("*/span[#id='time2']");
$time3 = $xpath->query("*/span[#id='time3']");
//$time4 = $xpath->query("*/span[#id='time4']");
$array = array(
$time1,$time2,$time3
);
// Save the output format
$DATE_FORMAT_STRING = "YmdHis";
// GET the current STAGE
$current_state = getState($array[0]->nodeValue);
$offset = 0;
$flag = 0;
foreach($array as $time)
{
echo $time->item(0)->nodeValue;
// Get the item state.
//$this_state = getState($time);
$this_state = getState($time->item(0)->nodeValue);
//echo $time->nodeValue;
// check if we past a day?
if($current_state == "PM" && $this_state == "AM")
{
$offset++;
}
$this_unix = strtotime($time->item(0)->nodeValue) + (60 * 60 * 24 * $offset);
$values[] = date($DATE_FORMAT_STRING, $this_unix);
//echo date($DATE_FORMAT_STRING, $this_unix);
echo $values[$count];
echo "<br></br>";
$starttime = $times->nodeValue;
//echo $starttime;
echo "<programme channel='".$my_id." ".$channel." start='".$starttime."' stop='".$stoptime."'>";
/*if($flag>0)
{
//echo "<programme channel='".$my_id." ".$channel." start='".$starttime."' stop='".$stoptime."'>";
$stoptime = $starttime;
$flag=1;
}
else
{
$stoptime = $starttime;
}*/
$current_state = $this_state;
$count++;
}
}
}
}
?>
My question is how do you write the simple way to write the code to make it shorter to get the element id of time1 to time69 using with few line of code?
Edit: I'm getting fatal error when I'm trying to print the list of strings.
The error are jumping on this line:
$time{$i} = $xpath->query("*/span[#id='time".$i."']");
It could be this:
echo $time->item(0)->nodeValue;
Fatal error: Cannot use object of type DOMNodeList as array in /home/myusername/public_html/work_on_this.php on line 57
Here is the update code:
for ($i = 1; $i < 70; $i++)
{
$time{$i} = $xpath->query("*/span[#id='time".$i."']");
$array = array(
$time{$i}
);
foreach($array as $time)
{
echo $time->item(0)->nodeValue;
}
}
Update the code to following,
$time_arr = array();
for ($i = 1; $i < 70; $i++){
$time_arr[] = $xpath->query("*/span[#id='time".$i."']");
}
foreach($time_arr as $time){
echo $time->item(0)->nodeValue;
}
I'm adding the values to an array directly in the for loop instead of assigning them separately.
And also foreach should not be within for loop which gets the time value.
Use a for loop:
for ($i = 1; $i < 70; $i++) {
$time{$i} = $xpath->query("*/span[#id = \"time\" . $i]");
}
I work with Silex Framework and I would like to add $age to $result after the calculation of the age
Is that possible??
$result = $app['db']->fetchAll($sql);
foreach ($result as $DN) {
$DT = $DN['dateN'];
$am = explode('-', $DT);
$an = explode('/', date('Y/m/d'));
$age = $an[0] - $am[0];
echo $age;
}
return $app['twig']->render('page.html',array('list' => $result,'age'=>$age));
You can try to use a reference (with &) instead of a copy and add the item:
foreach ($result as &$DN) {
$DT = $DN['dateN'];
$am = explode('-', $DT);
$an = explode('/', date('Y/m/d'));
$age = $an[0] - $am[0];
$DN['age'] = $age;
}
Assuming your fetchAll returns an array of arrays "referencing" your resultset
foreach($result as $index => $row) {
// ....
$result[$index] = $age;
}
I've got the following sql query:
$sql = "SELECT lat, lang
FROM users";
I then use the following code to put the results of the array into two arrays, one for lat and one for lang.
$i = 0;
foreach($results as $row) {
$latArray = array();
$langArray = array();
$latArray[$i] = $row['lat'];
$langArray[$i] = $row['lang'];
$i = ($i + 1);
}
However, it seems that only the last value that is passed to the array is stored. When I echo out each value of the array I get the following error: Undefined offset: 0 which I believe means theres nothing at latArray[0].
I'm sure I've missed something obvious here but why aren't all the values copied to the new array?
$i = 0;
$latArray = array(); //Declare once, do not redeclare in the loop
$langArray = array();
foreach($results as $row) {
$latArray[$i] = $row['lat'];
$langArray[$i] = $row['lang'];
$i = ($i + 1);
}
Declare your array before the loop
$latArray = array();
$langArray = array();
foreach($results as $row) {
$latArray[$i] = $row['lat'];
$langArray[$i] = $row['lang'];
$i = ($i + 1);
}
You should put
$latArray = array();
$langArray = array();
Before foreach cycle (like you do with your counter $i), 'cause with every new value from $result it resets thous values..
so your code will look like:
$i = 0;
$latArray = array();
$langArray = array();
foreach($results as $row) {
$latArray[$i] = $row['lat'];
$langArray[$i] = $row['lang'];
$i = ($i + 1);
}
use fetch_assoc instead:
$latArray = array();
$langArray = array();
while($row = mysql_fetch_assoc($your_query)){
$latArray[$i] = $row['lat'];
$langArray[$i] = $row['lang'];
$i++;
}
if u are using msqli us mysqli_fetch_assoc
I want to show on first page randomly selected links from a dati.txt file (from about 1000 links 5 randomly selected) from this:
<?php
$righe_msg = file("dati.txt");
$numero=0;
foreach($righe_msg as $riga)
{
$dato = explode("|", trim($riga));
if (trim($dato[5])=="yes")
{
$numero++;
echo"<tr><td bgcolor='#EEEEEE'>» <a href='$dato[2]'> $dato[1]</a></td></tr> ";
}
}
?>
About dati.txt
dati.txt is made like this
date1.. |title1..|link1...|description1|email1|yes
date2.. |title2..|link2...|description2|email2|yes
date3.. |title3..|link3...|description3|email3|yes
date4.. |title4..|link4...|description4|email4|yes
date5.. |title5..|link5...|description5|email5|yes
date6.. |title6..|link6...|description6|email6|yes
..
But how do you get for example (links) out with this code:
$links = file("dati.txt");
$numLinks = count($links);
$tmp = array();
for ($i = 0; $i < min(5, $numLinks); $i++)
{
$randomIndex = rand(0, $numLinks - 1);
$randomLink = $links[$randomIndex];
// Avoid duplicates:
if (in_array($randomLink, $tmp))
{
$i--;
continue;
}
$tmp[] = $randomLink;
echo $randomLink;
}
Thanks
<?php
$links = file('links.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// used this for testing (generated from a file with one link each line (+carriage return)
//$links = ARRAY('foo 1','foo 2','foo 3','foo 4','foo 5','foo 6','foo 7','foo 8','foo 9','foo 10','foo 11','foo 12');
$amount = 3;
shuffle($links);
$rand_list = array_slice($links, 0, $amount);
foreach ($rand_list AS $key => $link) {
print $link.'</br>';
}
?>
Considering 1 link per line:
$links = file("dati.txt");
$numLinks = count($links);
$randomIndex = rand(0, $numLinks - 1);
$randomLink = $links[$randomIndex];
echo $randomLink;
Getting more links is simply a matter of a loop:
$links = file("dati.txt");
$numLinks = count($links);
$tmp = array();
for ($i = 0; $i < min(5, $numLinks); $i++)
{
$randomIndex = rand(0, $numLinks - 1);
$randomLink = $links[$randomIndex];
// Avoid duplicates:
if (in_array($randomLink, $tmp))
{
$i--;
continue;
}
$tmp[] = $randomLink;
echo $randomLink;
}
$righe_msg = file("dati.txt");
$num = 5;
$selected = array();
while (count($selected)!=$num)
{
$rand = rand(0,count($righe_msg)-1);
$selected[$rand] = $righe_msg[$rand];
}
$numero=0;
foreach ($selected as $sel)
{
$dato = explode("|", trim($sel));
if (trim($dato[5])=="yes")
{
$numero++;
echo"<tr><td bgcolor='#EEEEEE'>» <a href='$dato[2]'> $dato[1]</a></td></tr> ";
}
}