I am trying to create an array from this data, but I donĀ“t get it. I tried with the array_merge function, but the array doesn't construct correctly. This is my code, I want to create an array with the different fields of the table.
<?php
require('extractorhtml/simple_html_dom.php');
$dom = new DOMDocument();
//load the html
$html = $dom->loadHTMLFile("http:");
//discard white space
$dom->preserveWhiteSpace = false;
//the table by its tag name
$tables = $dom->getElementsByTagName('table');
//get all rows from the table
$rows = $tables->item(0)->getElementsByTagName('tr');
echo '<input type="text" id="search" placeholder="find" />';
echo '<table id="example" class="table table-bordered table-striped display">';
echo '<thead>';
echo '<tr>';
echo '<th>Date</th>';
echo '<th>Hour</th>';
echo '<th>Competition</th>';
echo '<th>Event</th>';
echo '<th>Chanel</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
// loop over the table rows
foreach ($rows as $row)
{
// get each column by tag name
$cols = $row->getElementsByTagName('td');
// echo the values
echo '<tr>';
echo '<td>'.$cols->item(0)->nodeValue.'</td>';
echo '<td>'.$cols->item(1)->nodeValue.'</td>';
echo '<td>'.$cols->item(3)->nodeValue.'</td>';
echo '<td class="text-primary">'.$cols->item(4)->nodeValue.'</td>';
echo '<td>'.$cols->item(5)->nodeValue.'</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
?>
You don't need to merge arrays, you just need to push onto a new array to create a 2-dimensional array.
$new_array = array();
foreach ($rows as $row)
{
// get each column by tag name
$cols = $row->getElementsByTagName('td');
// echo the values
echo '<tr>';
echo '<td>'.$cols->item(0)->nodeValue.'</td>';
echo '<td>'.$cols->item(1)->nodeValue.'</td>';
echo '<td>'.$cols->item(3)->nodeValue.'</td>';
echo '<td class="text-primary">'.$cols->item(4)->nodeValue.'</td>';
echo '<td>'.$cols->item(5)->nodeValue.'</td>';
echo '</tr>';
$new_array[] = array(
'date' => $cols->item(0)->nodeValue,
'hour' => $cols->item(1)->nodeValue,
'competition' => $cols->item(3)->nodeValue,
'channel' => $cols->item(5)->nodeValue
);
}
Based on your <th> values, you know which columns contain which values, so it looks like you'd just need to modify the code inside your foreach loop to append the values to an array rather than generating new HTML with them.
foreach ($rows as $row)
{
// get each column by tag name
$cols = $row->getElementsByTagName('td');
$array['date'] = $cols->item(0)->nodeValue;
$array['hour'] = $cols->item(1)->nodeValue;
$array['competition'] = $cols->item(3)->nodeValue;
$array['event'] = $cols->item(4)->nodeValue;
$array['chanel'] = $cols->item(5)->nodeValue;
$result[] = $array;
}
After this loop, $result will be an array of arrays containing the values from the <td>s, where each inner array represents one <tr>.
**Edit: We got there in the end, Thanks guys! It was the HTML tables confusing me.
<tr>
<td><?php echo $row['owner_firstname'];?></td>
<td><?php echo $row['owner_surname'];}?></td>
</tr>**
I am trying to put some information into a table and I don't want to do it using this method...
<tr>
<td><?php echo $rows[0]['owner_firstname'] ; ?></td>
<td><?php echo $rows[0]['owner_surname'] ; ?></td>
<td><?php echo $rows[0]['owner_contantno'] ; ?></td>
</tr>
I want to use a foreach loop but I am struggling to get it working, Each person from the database is [0],[1],[2] etc in my array.
Here is a print_r of my dataset
Array
(
[0] => Array
(
[ID] => LEI12345
[owner_firstname] => Shanel
[owner_surname] => **********
[owner_contantno] => *******
[owner_address] => ********
[band_firstname] => Nathan
[band_lastname] => **********
[band_disability] => *******
[band_emergencycontact] => ********
[band_description] => ************
)
)
$data = $yourDataSet; // your data set here
// check data
if($data) {
foreach($data as $val) {
$str = "";
$str = "<tr>";
$str .= "<td>" . $val['owner_firstname'] . "</td>";
$str .= "<td>" . $val['owner_surname'] . "</td>";
// add other td here if there's more
// end of tr
$str .= "</tr>";
echo $str;
}
}
try this one, i hope this one would help
from your for each you can grab results like this
get your results to an array
$rows = mysql_fetch_array($query);
foreach($rows as $key=> $row){
if(is_array($row))
foreach($row as $id => $val)
echo '<td>'.$val.'</td>';
}
I want to take the result of foreach in a table format and that whole table in a variable.
Here is my model:
public function cron_job_seller(){
$this->db->select('*');
$this->db->from('wc_seller_products');
$query = $this->db->get();
$result = array();
foreach ($query->result() as $row){
$result[] = $row;
}
return $result;
}
and my controller is
public function cron_job(){
$this->load->model('home/Home_model');
$buyer = $this->Home_model->cron_job_buyer();
$this->load->library('email', array('mailtype'=>'html'));
$seller = $this->Home_model->cron_job_seller();
echo "<table>";
foreach($seller as $key=>$row) {
echo "<tr>";
foreach($row as $key2=>$row2){
echo "<td>" . $row2 . "</td>";
}
echo "</tr>";
}
echo "</table>";
gives o/p like this in table format
19 102 Rolex 65 Good 0000-00-00 fh ghf fgh ghf ghf gfh ghf ghf 56 56 download14.jpg 11/6/2016 19:03 2016-07-15 12:13:35 1 0
when i print $seller variable it gives
Array ( [0] => stdClass Object ( [id] => 19 [seller_id] => 102 [brand_name] => Rolex [model_no] => 65 [condition] => Good [date_purchase] => 0000-00-00 [case_size] => fh [case_shape] => ghf [case_material] => fgh [strap_type] => ghf [dial_colour] => ghf [water_resistance] => gfh [local_overseas] => ghf [warranty_period] => ghf [min_price] => 56 [sale_price] => 56 [photo] => download14.jpg [date] => 11/6/2016 19:03 [time] => 2016-07-15 12:13:35 [status] => 1 [login] => 0 [verified] => )
now i want 2 things in this:
1. that whole table in a single variable.
2. array keys like id, seller_id,brand_name as table heading
i really got confused what to do now and how to do...please help
public function cron_job() {
$this->load->model('home/Home_model');
$buyer = $this->Home_model->cron_job_buyer();
$this->load->library('email', array('mailtype'=>'html'));
$seller = $this->Home_model->cron_job_seller();
$theader = '';
$tbody = "<tbody>";
foreach($seller as $key => $row) {
$tbody .= "<tr>";
foreach($row as $key2 => $row2){
if (!$theader) {
$theader = array_keys($row2);
}
$tbody .= "<td>" . $row2 . "</td>";
}
$tbody .= "</tr>";
}
$tbody .= "</tbody>";
if (!empty($theader)) {
$theader = '<thead><th>' . implode('</th><th>', $theader) . '</th></thead>';
}
$table = '<table>'.$theader.$tbody.'</table>';
}
you don't need to worry much about it... It could be a solution. Just do like this in your controller:
$seller = $this->Home_model->cron_job_seller();
$table = "<table border='1'>";
$i=1;
foreach($seller as $key=>$row) { //you don't need to loop twice
if($i == 1){
$table.="<tr>";
$table .= "<th>".$key."</th>";
$table.="</tr>";
$i=0; //change value of $i so for next iteration the header will not print
}
$table .= "<tr>";
$table .= "<td>" . $row . "</td>";
$table.= "</tr>";
}
$table .= "</table>";
and also you need to return $query->result() from your model.
and now try print $table
using echo $table; it will print result as you want and now it is also set in a single variable so you can, and you set to $data['table'] = $table and end it to view, if you need.
You can just loop through the Array of Data from the DB and since each row is an array of Standard PHP Object (which contains the column names as keys), you can then extract the Column Names from the first row and then use it to construct the Table Header. Afterwards, you can just continue building Rows containing the actual Data you want. The Code below illustrates how:
<?php
function cron_job() {
$strOutput = "<table class='cron-tbl'>";
$this->load->model('home/Home_model');
$buyer = $this->Home_model->cron_job_buyer();
$this->load->library('email', array('mailtype' => 'html'));
$seller = $this->Home_model->cron_job_seller();
$cue = 0;
foreach ($seller as $key => $row) {
if($cue == 0){
// CREATE THE HEADER ROW:
$strOutput .= "<tr class='cron-head-row'>" . PHP_EOL;
foreach($row as $rowName=>$rowVal){
$strOutput .= "<th class='cron-head-cell'>{$rowName}</th>" . PHP_EOL;
}
$strOutput .= "</tr>" . PHP_EOL;
$strOutput .= "<tbody class='cron-body'>" . PHP_EOL;
}
// CREATE THE TABLE-BODY CELL
$strOutput .= "</tr>" . PHP_EOL;
foreach ($row as $rowKey => $data) {
$strOutput .= "<td class='cron-data-cell'>{$data}</td>" . PHP_EOL;
}
$strOutput .= "</tr>" . PHP_EOL;
$cue++;
}
$strOutput .= "</tbody>" . PHP_EOL;
$strOutput .= "</table>" . PHP_EOL;;
return $strOutput;
}
echo (cron_job());
Cheers & Good Luck...
Test it out yourself HERE.
You are doing correct but need some modifications
What you need to do is in you second foreach() loop you need to convert your object to array and take out the array keys array_keys($array) in a variable and print this keys to variable. As shown bellow.
$table = "<table border='1'>";
foreach($seller as $key=>$row) { //you don't need to loop twice
$table .="<tr>";
// creating a table heading
if($key === 0 ){
$cols = array_keys((array)$row); // Convert the object to array and take out all keys of array and assign to $cols variable
$table .="<th>".implode("</th><th>", $cols);
$table .="</th></tr><tr>";
}
// table heading end
$table .= '<td>'. implode('</td><td>', (array)$row). '</td>';
$table .= "</tr>";
}
$table .="</table>";
echo $table;
Try this way.
I guess html tag, body tag are missing. That's why didnot display table the way you want.
My suggestion is that for html part, it is better if put in view.
I have create view page cron_job_seller.php with bootstrap.
// Model - Change to this
public function cron_job_seller()
{
$this->db->select('*');
$this->db->from('wc_seller_products');
return $this->db->get();
}
// Controller
public function cron_job() {
$this->load->model('home/Home_model');
$this->load->library('email', array('mailtype'=>'html'));
$buyer = $this->Home_model->cron_job_buyer();
$seller = $this->Home_model->cron_job_seller();
$data['seller'] = $seller // Send data to View
// Create cron_job_seller.php file in Views Folder.
$this->load->view('cron_job_seller', $data);
}
// View cron_job_seller.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sample Table</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<table class="table table-bordered">
<?php foreach($seller->result() as $row) { ?>
<tr>
<td><?php echo $row->colname_1; ?></td>
<td><?php echo $row->colname_2; ?></td>
<td><?php echo $row->colname_3; ?></td>
<td><?php echo $row->colname_4; ?></td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>
I want to create a Bingo script. I have already coded a 6x6 pattern with random numbers which have a certain range on each line. BUT now it outputs the same number multiple times, i want it to be all random numbers, can someone help me out?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Indzendopdracht 051R3</title>
</head>
<body>
<?PHP
function printBingocard(){
$bingoNumbers = array(
"rij_1" => array(10,11,12,13,14,15,16,17,18,19),
"rij_2" => array(20,21,22,23,24,25,26,27,28,29),
"rij_3" => array(30,31,32,33,34,35,36,37,38,39),
"rij_4" => array(40,41,42,43,44,45,46,47,48,49),
"rij_5" => array(50,51,52,53,54,55,56,57,58,59),
"rij_6" => array(60,61,62,63,64,65,66,67,68,69)
);
$rand_keys = array_rand($bingoNumbers, 1);
$rows = array();
foreach($bingoNumbers["rij_1"] as $bn_rij1){
if($bn_rij1 > 10 && $bn_rij1 <= 16){
$rows["row1"][]="<td>" . $bingoNumbers["rij_1"][array_rand($bingoNumbers["rij_1"])] ."</td>";
}
}
foreach($bingoNumbers["rij_2"] as $bn_rij2){
if($bn_rij2 > 20 && $bn_rij2 <= 26){
$rows["row2"][]="<td>" . $bingoNumbers["rij_2"][array_rand($bingoNumbers["rij_2"])] ."</td>";
}
}
foreach($bingoNumbers["rij_3"] as $bn_rij3){
if($bn_rij3 > 30 && $bn_rij3 <= 36){
$rows["row3"][]="<td>" . $bingoNumbers["rij_3"][array_rand($bingoNumbers["rij_3"])] ."</td>";
}
}
foreach($bingoNumbers["rij_4"] as $bn_rij4){
if($bn_rij4 > 40 && $bn_rij4 <= 46){
$rows["row4"][]="<td>" . $bingoNumbers["rij_4"][array_rand($bingoNumbers["rij_4"])] ."</td>";
}
}
foreach($bingoNumbers["rij_5"] as $bn_rij5){
if($bn_rij5 > 50 && $bn_rij5 <= 56){
$rows["row5"][]="<td>" . $bingoNumbers["rij_5"][array_rand($bingoNumbers["rij_5"])] ."</td>";
}
}
foreach($bingoNumbers["rij_6"] as $bn_rij6){
if($bn_rij6 > 60 && $bn_rij6 <= 66){
$rows["row6"][]="<td>" . $bingoNumbers["rij_6"][array_rand($bingoNumbers["rij_6"])] ."</td>";
}
}
echo "<table>";
foreach($rows as $row){
echo "<tr>";
foreach($row as $r){
echo $r;
}
echo "</tr>";
}
echo "</table>";
}//END OF FUNCTION
printBingocard();
?>
</body>
</html>
Why not just use shuffle()? As is right now, you're never checking for array_rand() producing a duplicate number, so yeah - you'll get dupes.
This is far more efficient:
foreach(array_keys($bingoNumbers) as $key) {
shuffle($bingoNumbers[$key]);
}
One SINGLE loop, and each of those sub arrays is shuffled, without duplicates, in far far less code.
$bingoNumbers = array(
"rij_1" => array(10,11,12,13,14,15,16,17,18,19),
"rij_2" => array(20,21,22,23,24,25,26,27,28,29),
"rij_3" => array(30,31,32,33,34,35,36,37,38,39),
"rij_4" => array(40,41,42,43,44,45,46,47,48,49),
"rij_5" => array(50,51,52,53,54,55,56,57,58,59),
"rij_6" => array(60,61,62,63,64,65,66,67,68,69)
);
foreach(array_keys($bingoNumbers) as $key)
shuffle($bingoNumbers[$key]);
echo "<table border='1'>";
foreach($bingoNumbers as $v)
echo "<tr><td>" . implode("</td><td>", $v) . "</td></tr>";
echo "</table>";
Demo
If you only want a 6x6 grid, just array_slice() your sub arrays, e.g.
$bingoNumbers = array(
"rij_1" => array(10,11,12,13,14,15,16,17,18,19),
"rij_2" => array(20,21,22,23,24,25,26,27,28,29),
"rij_3" => array(30,31,32,33,34,35,36,37,38,39),
"rij_4" => array(40,41,42,43,44,45,46,47,48,49),
"rij_5" => array(50,51,52,53,54,55,56,57,58,59),
"rij_6" => array(60,61,62,63,64,65,66,67,68,69)
);
foreach(array_keys($bingoNumbers) as $key)
shuffle($bingoNumbers[$key]);
echo "<table border='1'>";
foreach($bingoNumbers as $v)
echo "<tr><td>" . implode("</td><td>", array_slice($v, 0, 6)) . "</td></tr>";
//^^^^^^^^^^^^ Just slice your sub array
echo "</table>";
Demo
I'm trying to display the results of my query in a neatly formatted table but I'm pretty new to PHP so I'm at a bit of a loss. Currently I have this:
$dbconn = pg_connect("host=localhost port=5432 dbname=mary");
$result = pg_query($dbconn, $selectStmt);
$resultArr = pg_fetch_all($result);
print_r($resultArr);
Assume $selectStmt = SELECT State, Name FROM perez.pop WHERE Name LIKE '%Alabama%';
When I print this, I get the following:
Array ( [0] => Array ( [state] => 1 [name] => Alabama ) )
How could I place this into a table where the columns are "state" and "name" along with one more column in which I plan to place a link to a different page?
Also, can anyone clarify how the $resultArr looks when I get multiple rows as a result of my query?
EDIT: I'd like the output to look something like this:
State | Name | Follow link
___________________________________________________
32 Alabama <some link to a php page>
2 Alabama <another link>
You can just loop in your array and print your table while looping, something like
$resultArr = pg_fetch_all($result);
//print_r($resultArr);
echo '<table>
<tr>
<td>State</td>
<td>Name</td>
</tr>';
foreach($resultArr as $array)
{
echo '<tr>
<td>'. $array['State'].'</td>
<td>'. $array['Name'].'</td>
</tr>';
}
echo '</table>';
$query = pg_query("SELECT * FROM user");
$users_arr = pg_fetch_all($query);
// render thead
$thead = '<thead>
<tr>';
foreach($users_arr[0] as $key => $value) {
$thead .= '<th>' . $key . '</th>';
}
$thead .= '</tr>
</thead>';
// render tbody
$tbody = '<tbody>';
foreach($users_arr as $key => $value) {
$tbody .= '<tr>';
foreach($value as $k => $v) {
$tbody .= '<td>' . $v . '</td>';
}
$tbody .= '</tr>';
}
$tbody .= '</tbody>';
// render table
$table = '<table>' . $thead . $tbody . '</table>';
echo $table;