how to pass array into database - php

I am trying to pass arrays into a database but can't get it right.
I have following db rows
Country, number, Page, date
And want something like this in my database
id Country number Page date
2 Sweden 5 cat 14th june 2015
United 10 ind 14th june 2015
states 58 con 14th june 2015
France 101 aces 14th june 2015
Germany 200 ind 14th june 2015
I am using this
if(isset($_POST['submit'])){
$new_res = mysql_query("INSERT INTO `em-res_db` VALUES('','$Country','$number','$Page','$date')");
if ($new_res){
//continue
}
I hope you get the picture of what I'm trying to do

Create a loop who have the same length as the array, and in the loop insert the code where you are inserting data to the db.
Example
for(i=0;i<=count(array)-1;i++){
if(isset($_POST['submit'])){
$new_res = mysql_query("INSERT INTO `em-res_db` VALUES('','$Country','$number','$Page','$date')");
if ($new_res){
//continue
}
}

Related

Phpspreadsheet - Time cell retrieved as float

I have an excel file which has a time input.
Mar 01, 2018 | Thursday | 8:00 AM | 5:00 PM
Mar 02, 2018 | Friday | 8:00 AM | 5:00 PM
But when my code tries to read those cells, the output becomes a float (for example 8:00 AM becomes 0.33333333333333). This is my code
$date = $sheet->getCell("A".$row)->getValue();
$time_in = $sheet->getCell("C".$row)->getValue();
$time_out = $sheet->getCell("D".$row)->getValue();
echo "date: ".$date. //Mar 01, 2018
" time_in: ".$time_in. //becomes 0.333333333333333
" time_out: ".$time_out; //becomes 0.708333333333333
How can I make the output as is without the phpspreadsheet changing the value? I have tried looking at the phpspreadsheet documentation but I haven't found a solution.
Thankfully I have found the answer just now.
$in = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($time_in);
echo gmdate("g:i a", $in);
Hopefully, this could be useful for others.
Formart the data before you read it.
For example:
cell data 2020/10/13 19:00:00
Access directly will get 44117.791666667
$sheet->toArray();
Format before access will get 2020-10-13 19:00:00
$row_num = $sheet->getHighestDataRow();
$sheet->getStyle("H1:H$row_num")->applyFromArray(array("numberFormat"=>array("formatCode"=>'yyyy-mm-dd hh:mm:ss'))); // H is the datatime row
$sheet->toArray();

How to turn a Postgres result with varying number of results into an ordered list in PHP

I have two tables, one with a list of countries; another with the list of years and values for a given variable.
The second table holds entries for certain years only, which differ for the countries. So that, when I join the tables (to get the name of the countries, not displayed in the following list) a result does look like this:
id_country year_start value_ori
886 2002 161.348
886 2003 161.348
886 2004 176.016
886 2005 176.016
886 2006 179.683
886 2007 183.35
886 2008 201.685
886 2009 227.354
886 2010 234.688
886 2011 245.689
886 2012 293.36
886 2013 440.04
620 1990 40.337
620 1991 1056.096
620 1992 1151.438
620 1993 1389.793
620 1994 1584.144
620 1995 1631.815
620 1996 1749.159
620 1997 1796.83
620 1998 1906.84
620 1999 1664.818
620 2000 1642.816
620 2001 2016.85
620 2002 1760.16
620 2003 1873.837
620 2004 1961.845
620 2005 2310.21
620 2006 2328.545
620 2007 2361.548
620 2008 3329.636
620 2009 3069.279
620 2010 3098.615
620 2011 2823.59
620 2012 3373.64
620 2013 2948.268
That is, for the country 886 I don't have values for earlier than 2002.
Now, I'd like to create a CSV with this list, which should list the values for each country in a row, one year after the next - like a table. Which means that if there are no values for certain years, there should be a blank space (or 'null'), as can be seen here:
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
620 40.337 1056.096 1151.438 1389.793 1584.144 1631.815 1749.159 1796.83 1906.84 1664.818 1642.816 2016.85 1760.16 1873.837 1961.845 2310.21 2328.545 2361.548 3329.636 3069.279 3098.615 2823.59 3373.64 2948.268
886 161.348 161.348 176.016 176.016 179.683 183.35 201.685 227.354 234.688 245.689 293.36 440.04
Now, I don't succeed in writing a loop, which would take care of those void 'values'. At least not a smooth one. I could retrieve all available years, and then either launch a query for each year and country. Or build a three dimensional array with the results, and use that one for looping. But that looks like weird programming.
I would appreciate any comment on how this can be achieved with a few clean lines of PHP.
Looping over the min and max of the years found while also looping (inner) over all the companies found will let you create the csv rows without missing any years nor companies:
$company_ids = array();
$company_data = array();
$year_min = $year_max = 2016;
$year_fld = 'year_start';
$comp_fld = 'id_country';
$data_fld = 'value_ori';
foreach ($data as &$d) {
$company_ids[$d[$comp_fld]] = $d[$comp_fld];
$d[$year_fld] = (integer) $d[$year_fld];
if ($year_min > $d[$year_fld]) $year_min = $d[$year_fld];
if ($year_max < $d[$year_fld]) $year_max = $d[$year_fld];
$company_data[$d[$year_fld]][$d[$comp_fld]] = $d[$data_fld];
}
//die("$year_min $year_max");
//ksort($company_data);
//die('<pre>'.print_r($company_data,true));
$csv_rows = array();
for ($year=$year_min; $year<$year_max+1; $year++) {
// print rows or accumulate them, whatever
foreach ($company_ids as &$cid) {
$csv_rows[$year][$cid] = (empty($company_data[$year][$cid])) ? '' : $company_data[$year][$cid];
}
}
die('<pre>'.print_r($csv_rows,true));

DOM returns wrong text

Im trying to make something which retrieves event data, but when it tries to echo some special tokens I get weird text back.
This is the code:
foreach( $new_html->find( 'div#desc' ) as $desc ) {
echo $desc;
echo "</br>";
}
and this is what it returns:
ClubCuccini Persian Valentines Party
▀▀▀▀ Friday 12th February 2016 ▀▀▀▀
ClubCuccini would like to invite you all to one of the most romantic and exciting events of the year in L8 Club , one of the most exclusive place in London.
but it should be returning:
▀▀▀▀ Friday 12th February 2016 ▀▀▀▀
ClubCuccini would like to invite you all to one of the most romantic and exciting events of the year in L8 Club , one of the most exclusive place in London.
Any ideas how to get the good text form?

distinct date mysql php year

I have problem with my php/mysql.
I want to get distinct year from my fields ex 2013-06-20.
Now I get something like this :
2014
2013
2014
2013
2013
2014
2014
2014
2014
2014
2014
2014
PHP CODE :
<?$chuj=mysql_query("SELECT DISTINCT date_issue FROM invoices_sales ");
while($chuje=mysql_fetch_object($chuj)){
$test=explode("-",$chuje->date_issue);
print_r($test['0']."<br>");
}
?>
How I can get only once 2013 or 2014 year ?
You need to grab the year from that particular date string.
SELECT DISTINCT YEAR(date_issue) FROM invoices_sales
This will ensure it's only comparing the year, as it stands, each of those strings likely have different dates, so it is evaluating the entire date as opposed to only the year.
<?php
$chuj=mysql_query("SELECT DISTINCT YEAR(`date_issue`) as `yr` FROM `invoices_sales`");
while($chuje=mysql_fetch_object($chuj)) {
$test = $chuje->yr;
print_r($test['0'] . "<br>");
}
?>

Need to display mysql table ordered by date but separated by week

I'm using PHP and MySQL. I have a page that displays meetings created by people with a link to view details. Right now I'm using just a simple table to display everything sorted by date. The table in the database is called 'meetings' and there are 3 columns -
'meetingid' (int)
'time' (datetime)
'creator' (text)
My problem is that it looks a little messy and difficult to read when there are quite a few meetings created since they are all in just one big clump. I'd like to split them up by week (starting Monday, ending Sunday - or Sunday-Saturday if that's easier). I've linked to a doc at the bottom showing what I currently have (first page) and something more like what I want (second page). The week labels (ex. September 3rd - September 9th) would need to only go on for as long as there are meetings booked. So, if the latest meeting is October 7th then the last week shown should be 'October 1st - October 7th'. Figuring out how to separate them by month seems easy enough but I can't wrap my head around how to do it by week. I'm assuming that there's some php date function that will help greatly with this but I can't find it. Hoping you all can help.
What is the best way to do this?
I haven't decided yet whether or not I'd want the weeks where there are no meetings to show the week label or not. (Ex. There are no meetings between September 10th - September 16th. - so do or do not show that label.
Link to examples (no need to sign into google)
https://docs.google.com/document/d/16cvRfPmovNBsx9QQ0U5qhVoW8bo0xjEABil3wTtEUrA/edit
Use date("W") to get the week number of the year.Then you can separate your results according to the week number.
Without knowing how your data is structured; you could use the week number returned by the date() function to keep track of which week you are in and break it up that way.
$currentWeekNumber = -1;
$rows = array(
array('id' => 5, 'started_by' => 'Ben', 'when' => '2012-09-06 09:00:00'),
array('id' => 6, 'started_by' => 'Julie', 'when' => '2012-09-07 18:00:00'),
array('id' => 18, 'started_by' => 'Ben', 'when' => '2012-09-18 20:00:00')
);
foreach($rows as $row) {
$eventSeconds = strtotime($row['when']);
$rowWeek = intval(date('W', $eventSeconds));
if( $rowWeek !== $currentWeekNumber && $currentWeekNumber !== -1) {
echo "----- Week Break ------\n";
}
$currentWeekNumber = $rowWeek;
echo "Meeting #{$row['id']}, started by {$row['started_by']}, occurs at ".strftime('%c', $eventSeconds)."\n";
}
Which produces the following output:
Meeting #5, started by Ben, occurs at Thu Sep 6 09:00:00 2012
Meeting #6, started by Julie, occurs at Fri Sep 7 18:00:00 2012
----- Week Break ------
Meeting #18, started by Ben, occurs at Tue Sep 18 20:00:00 2012

Categories