-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
advise on getting dates in month/mini calendar
Barand replied to jonny512379's topic in PHP Coding Help
Have a look at the DateTime, DateInterval and DatePeriod classes DateTime DateInterval DatePeriod -
We have no knowledge of the structure of your data. Is there anything in the data to indicate that 4B, 8B and 9B are subsections of 3B, as in you second list above?
-
try $h = str_get_html($html); $res = $h->find("div.at_result[source=1215]"); foreach ($res as $r) { echo $r->category.'<br>'; } http://simplehtmldom.sourceforge.net/manual.htm
-
not difficult. $users = array ( array ('id' => 1, 'name' => 'Peters, John', 'age' => 22), array ('id' => 2, 'name' => 'Paul', 'age' => 22), array ('id' => 3, 'name' => 'Mary', 'age' => 22) ); $fp = fopen('users.csv', 'w'); $first = 1; foreach ($users as $udata) { if ($first) { // write header record to csv $heads = array_keys($udata); fputcsv($fp, $heads); $first = 0; } fputcsv($fp, $udata); } fclose($fp); Going the other way, use fgetcsv() to read each line of data into an array
-
Need help to select the funder with the second highest percentage
Barand replied to jmp98's topic in PHP Coding Help
You are not expected to just copy my code and paste it into yours. The purpose of the code was to set up a situation similar to yours and show you an example of how you can process it. -
Weird! I started with mysql> select * from logs order by usern,passwd,id,auth,date; +--------+---------+------+------+------------+--------+--------+ | usern | passwd | id | auth | date | active | number | +--------+---------+------+------+------------+--------+--------+ | aaaaaa | xxxxxxx | 1 | A | 2015-09-04 | 1 | 4 | | aaaaaa | xxxxxxx | 1 | C | 2015-09-01 | 1 | 1 | | aaaaaa | xxxxxxx | 1 | C | 2015-09-02 | 1 | 2 | | aaaaaa | xxxxxxx | 1 | C | 2015-09-03 | 1 | 3 | | aaaaaa | xxxxxxx | 1 | C | 2015-09-05 | 1 | 5 | | bbbbbb | yyyyyyy | 2 | A | 2015-09-01 | 1 | 6 | | bbbbbb | yyyyyyy | 2 | A | 2015-09-02 | 1 | 7 | | bbbbbb | yyyyyyy | 2 | A | 2015-09-03 | 1 | 8 | | bbbbbb | yyyyyyy | 2 | A | 2015-09-04 | 1 | 9 | | cccccc | zzzzzzz | 3 | A | 2015-09-06 | 1 | 10 | +--------+---------+------+------+------------+--------+--------+ then I ran your version of the query and was left with +--------+---------+------+------+------------+--------+--------+ | usern | passwd | id | auth | date | active | number | +--------+---------+------+------+------------+--------+--------+ | aaaaaa | xxxxxxx | 1 | A | 2015-09-04 | 1 | 4 | | aaaaaa | xxxxxxx | 1 | C | 2015-09-05 | 1 | 5 | | bbbbbb | yyyyyyy | 2 | A | 2015-09-04 | 1 | 9 | | cccccc | zzzzzzz | 3 | A | 2015-09-06 | 1 | 10 | +--------+---------+------+------+------------+--------+--------+
-
If you are writing to a db table CREATE TABLE `node` ( `id` bigint(20) NOT NULL, `amenity` varchar(30) DEFAULT NULL, `name` varchar(100) DEFAULT NULL, `housenumber` varchar(5) DEFAULT NULL, `housename` varchar(50) DEFAULT NULL, `street` varchar(50) DEFAULT NULL, `city` varchar(50) DEFAULT NULL, `postcode` varchar(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; this code will do it $jsn = 'INSERT YOUR DATA HERE'; $jsn = str_replace('addr:','',$jsn); // legalise db column names $data = json_decode($jsn,1); // create array from json data $template = array ( // template array for db inserts 'id' => '', 'amenity' => '', 'name' => '', 'housenumber' => '', 'housename' => '', 'street' => '', 'city' => '', 'postcode' => '' ); $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); // prepare the sql statement $sql = "INSERT INTO node (id, amenity, name, housenumber, housename, street, city, postcode) VALUES (?,?,?,?,?,?,?,?)"; $stmt = $db->prepare($sql); $stmt->bind_param('isssssss',$id, $amenity, $name, $housenumber, $housename, $street, $city, $postcode); // process the data foreach ($data['elements'] as $elem) { $tags = array_intersect_key($elem['tags'],$template); // get the tags we need $nodedata = array_merge($template,$tags); // add tag data to template array $nodedata['id'] = $elem['id']; // add id to template array extract($nodedata); $stmt->execute(); // insert in db table "node" }
-
As long as you carry on trying to process it as XML you are going to have a problem. Think "JSON". Use json_decode
-
Had you checked for errors you could have saved yourself a day. if($result){ echo "Successful"; echo "<BR>"; echo "<a href='insert.php'>Back to main page</a>"; } else { die (mysql_error()); }
-
$marray = array( '1' => 'apple,', '2' => 'orange,', '3' => ' ', '4' => ' ', '5' => 'apple'); $tmp = array_filter(array_map('trim', $marray)); // remove blanks $str = ''; foreach ($tmp as $k=>$v) { $str .= "$k. $v "; } echo $str; //==> 1. apple, 2. orange, 5. apple [edit] Damn! Beaten to the post.
-
I substituted your column and table names for you. (This will remove duplicates of user/passwd/id and keep the latest) DELETE logs FROM logs INNER JOIN ( SELECT number , @seq := IF(CONCAT(usern, passwd, id)<>@prevvals, 1, @seq+1) as seq , @prevvals := CONCAT(usern, passwd, id) as prev FROM logs JOIN (SELECT @prevvals:=null, @seq:=0) as init ORDER BY usern, passwd, id, date DESC ) as count USING (number) WHERE seq > 1;
-
What is in $_GET['p'] at that point. That is what determines which records are displayed.
-
Need help to select the funder with the second highest percentage
Barand replied to jmp98's topic in PHP Coding Help
this will get your top 3 class myItem { public $id; public $goal; public $total; public function __construct($id, $total, $goal) { $this->id = $id; $this->total = $total; $this->goal = $goal; } } // array of objects $list = array( new myItem('a', 10, 100), new myItem('b',2,10), new myItem('c',20,50), new myItem('d',1.5,10), new myItem('e',3,5), new myItem('f',5,100), new myItem('g',33,100) ); //sort the array by percent DESC usort ($list, function($a, $b) { $apc = 100*$a->total/$a->goal; $bpc = 100*$b->total/$b->goal; return $bpc - $apc; }); // get the first three elements $top3 = array_slice($list,0,3); // list result echo '<pre>'; foreach ($top3 as $item) { printf("%s %2.0f%%\n", $item->id, $item->total / $item->goal * 100); } echo '</pre>'; gives e 60% c 40% g 33% -
$next =++$page; increments $page then assigns that value to $next, so if $page was 1 it get incremented to 2 then that is assigned to next
-
What is the primary key on that table?
-
You're not too good at reading either. I gave you a query that will do it. Apply the same method using your table and column names
-
You don't specify what constitutes a duplicate so here is an example of how to do it which you can apply to your table. When you have deleted your duplicates, put a UNIQUE key on the relevant column/s to stop it happening again. My table mysql> SELECT * FROM articles ORDER BY category_id; +------------+-------------+---------+ | article_id | category_id | title | | (PK) | | | +------------+-------------+---------+ | 1 | 1 | Art 1 1 | | 2 | 1 | Art 1 2 | | 3 | 1 | Art 1 3 | | 4 | 2 | Art 2 1 | | 6 | 3 | Art 3 1 | | 7 | 3 | Art 3 2 | | 8 | 3 | Art 3 3 | | 9 | 3 | Art 3 4 | | 5 | 4 | Art 4 1 | +------------+-------------+---------+ 9 rows in set (0.00 sec) Task: delete duplicate category_ids The query uses a subquery to allocate a sequence number to each row withing each category so we can then delete those with a sequence number > 1 DELETE articles FROM articles INNER JOIN ( SELECT article_id , @seq := IF(category_id<>@prevcat, 1, @seq+1) as seq , @prevcat := category_id as category_id FROM articles JOIN (SELECT @prevcat:=null, @seq:=0) as init ORDER BY category_id ) as count USING (article_id) WHERE seq > 1 results mysql> SELECT * FROM articles; +------------+-------------+---------+ | article_id | category_id | title | +------------+-------------+---------+ | 1 | 1 | Art 1 1 | | 4 | 2 | Art 2 1 | | 5 | 4 | Art 4 1 | | 6 | 3 | Art 3 1 | +------------+-------------+---------+ 4 rows in set (0.00 sec)
-
I always used the "ReturnDatesAsStrings=true" option when connecting to sql server.
-
You're welcome Newbie.
-
It's a confusing data model that you have there. That implies that each prospect can have many territories, but it seem more likely that a territory would have many prospects and the model would be more like this +----------------------+ +--------------+ | Prospects | | Territory | +----------------------+ +--------------+ | ProspectCode | +-------| TerritoryID | | ProspectBusinessName | | | County | | ProspectLastContacted| | | State | | TerritoryID |>----+ | Zip | +----------------------+ +--------------+
-
try SELECT * FROM Prospects p LEFT JOIN Territories t ON (ProspectCounty = Territory AND ProspectState = TerritoryState) OR (ProspectZip = TerritoryZip) WHERE DATE(ProspectLastContacted) < CURDATE() - INTERVAL 14 DAY ORDER BY p.BusinessName
-
"column" is a reserved word - avoid using for column or table names. If you do use it then it needs to be in backticks $sql = "SELECT id, `column` FROM databasetable";
-
How to save id with the selected answer in php?
Barand replied to dapcigar's topic in PHP Coding Help
see http://forums.phpfreaks.com/topic/298080-quiz-results-passing-displaying-wrong/?do=findComment&comment=1520505 -
or make use of a custom sort function with usort() function sort_array(&$menu, $key, $order='ASC') { usort($menu, function($a, $b) use ($key, $order) { $res = strcmp($a[$key], $b[$key]); return ($order=='DESC') ? -$res : $res; }); }