I use simple-php-dom to get some html from other website.
Then I use foreach to find 3 things : business name, address and phone.
Finally I want to print those 3 info in my table using php loop.
Question : How to write the loop inside the table ?
Here's my PHP code to get the 3 info from other site :
<?php
//Get Biz Name
foreach($html->find('a[class=biz-name js-analytics-click]') as $biz_name_root){
foreach ($biz_name_root->find('span') as $biz_name) {
echo $biz_name->plaintext . "<br>";
}
}
//Get Address
foreach($html->find('address') as $address){
echo $address . '<br>';
}
// Get Phone
foreach($html->find('span[class=biz-phone]') as $phone){
echo $phone . '<br>';
}
?>
Here's the table that I want to store those 3 information to :
I want to store the $biz_name->plaintext to <td>biz_name</td>, the $address to
<td>address</td> and $phone to <td>phone</td>field.
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">No</th>
<th scope="col">Biz Name</th>
<th scope="col">Address</th>
<th scope="col">Phone</th>
</tr>
</thead>
<tbody>
<?php
for ($i=1; $i < count($html->find('address'))+2 ; $i++) { ?>
<tr>
<th scope="row"><?php echo $i ?></th>
<td>biz_name</td>
<td>address</td>
<td>phone</td>
</tr>
<?php }
?>
</tbody>
</table>
You should store all found info in arrays to iterate through them:
<?php
//Get Biz Name
$bisNames = array();
foreach($html->find('a[class=biz-name js-analytics-click]') as $biz_name_root){
foreach ($biz_name_root->find('span') as $biz_name) {
$bizNames[] = $biz_name->plaintext;
}
}
//Get Address
$adresses = array();
foreach($html->find('address') as $address){
$adresses[] = $address;
}
// Get Phone
$phones = array();
foreach($html->find('span[class=biz-phone]') as $phone){
$phones[] = $phone;
}
?>
And then:
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">No</th>
<th scope="col">Biz Name</th>
<th scope="col">Address</th>
<th scope="col">Phone</th>
</tr>
</thead>
<tbody>
<?php
for ($i=1; $i < count($html->find('address'))+2 ; $i++) { ?>
<tr>
<th scope="row"><?php echo $i ?></th>
<td><?php echo $bizNames[$i]; ?></td>
<td><?php echo $adresses[$i]; ?></td>
<td><?php echo $phones[$i]; ?></td>
</tr>
<?php }
?>
</tbody>
</table>
Related
Good day all,
I need help in project table
I have a table called projects and i want to count the projects based on country_id column using php in a foreach... ist possible?
however i am using cakephp the old version 1.2 ...
I tried this code:
<table>
<tr>
<th>Country </th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
$count_country =0;
$country_count_each=0;
foreach ($projects as $project){
$country_count_each = $project['Project']['country_id'];
if($project['Project']['country_id']==$country_count_each){
$count_country+=$country_count_each;
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'.$tt; ?></td>
<td style="width: 30%"><?php echo $count_country; ?></td>
</tr>
<?php
}
}
?>
</table>
You need to loop over the entire array and get all the counts first. You can put them into an associative array:
$country_counts = [];
foreach ($projects as $project) {
$country = $project['Project']['country_id'];
if (isset($country_counts[$country])) {
$country_counts[$country]++;
} else {
$country_counts[$country] = 1;
}
}
?>
<table>
<tr>
<th>Country </th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
foreach ($projects as $project) {
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'.$tt; ?></td>
<td style="width: 30%"><?php echo $country_counts[$project['Project']['country_id']]; ?></td>
</tr>
<?php
}
?>
</table>
I need help with how to remove the duplication within list of countries based on country_id.
I have a table which includes many projects for a country and counting the projects number which is working fine but need to print without duplication, only one country_id with the count of its project.
The code has been added - if anyone can help it is appreciated
<table>
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
$country_counts = [];
foreach ($projects as $project) {
$country_id = $project['Project']['country_id'];
if (isset($country_counts[$country_id])) {
$country_counts[$country_id]++;
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'; ?></td>
<td style="width: 30%"><?php echo $country_counts[$project['Project']['country_id']]; ?></td>
</tr>
<?php
} else {
$country_counts[$country_id] = 1;
}
}
?>
</table>
the result which i got is
shows the result
Do something like this
$checkout_array = array_unique($checkout_array, SORT_REGULAR);
I did this for my own you can do it for your array
I am unable to test this code but the idea of what I suggested as the second options goes as follows:
<table>
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
$country_counts=[];
$ids=array();// Store unique country_id values here
foreach( $projects as $project ) {
$country_id = $project['Project']['country_id'];
# Check if the country_id is NOT in the array and display if OK.
if( isset( $country_counts[ $country_id ] ) && !in_array( $country_id, $ids ) ) {
$country_counts[$country_id]++;
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'; ?></td>
<td style="width: 30%"><?php echo $country_counts[$project['Project']['country_id']]; ?></td>
</tr>
<?php
// add the country_ID to the array so that
// on the next iteration of the loop the $ids
// array will be checked again and no output if
// it is already in the array.
$ids[]=$country_id;
} else {
$country_counts[$country_id] = 1;
}
}
?>
</table>
While loop not working not showing anything.
My code is working when I use without creating a function on the same page but when I create function on functions.php and call it, it does not work.
<?php
include "include/db.php";
function search(){
global $connection;
$record_search = $_GET['search'];
$record_search = mysqli_real_escape_string($connection, $record_search);
$query_search = "SELECT * FROM users WHERE u_name='$record_search' OR u_roll='$record_search' ";
$result_search = mysqli_query($connection, $query_search);
if(!$result_search){
die("FAIL" . mysqli_error($connection));
}
while ($row_search = mysqli_fetch_array($result_search)) {
$name_search = $row_search[1];
$f_search = $row_search[2];
$school_search = $row_search[3];
$roll_search = $row_search[4];
$email_search = $row_search[5];
$class_search = $row_search[6];
}
}
?>
and
<?php
if(isset($_GET['search'])){
global $connection;
global $result_search;
search();
?>
<table class="table">
<thead>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Father Name</th>
<th>School</th>
<th>Roll No</th>
<th>Email</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td><?php echo #$name_search; ?></td>
<td><?php echo #$f_search; ?></td>
<td><?php echo #$school_search; ?></td>
<td><?php echo #$roll_search; ?></td>
<td><?php echo #$email_search; ?></td>
<td><?php echo #$class_search; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
You can see nothing happen:
Did you try to connect to the mysql before the execution of the query. Maybe, you have put the connect code in the other file, however couldn't see it here so just wondering.
your code should be like below, hope it will work to you.
function search(){
global $connection;
$record_search = $_GET['search'];
$record_search = mysqli_real_escape_string($connection, $record_search);
$result_search = mysqli_query($connection, $query_search);
if(!$result_search){
die("FAIL" . mysqli_error($connection));
}
$query_search = "SELECT * FROM users WHERE u_name='$record_search' OR u_roll='$record_search' ";
return $row_search = mysqli_fetch_array($result_search);
}
<table class="table">
<thead>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Father Name</th>
<th>School</th>
<th>Roll No</th>
<th>Email</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<?php
$search_results = search();
$i=1;
foreach($search_results as $row_search){
$name_search = $row_search[1];
$f_search = $row_search[2];
$school_search = $row_search[3];
$roll_search = $row_search[4];
$email_search = $row_search[5];
$class_search = $row_search[6];
?>
<tr>
<th scope="row"><?php echo $i ?></th>
<td><?php echo $name_search; ?></td>
<td><?php echo $f_search; ?></td>
<td><?php echo $school_search; ?></td>
<td><?php echo $roll_search; ?></td>
<td><?php echo $email_search; ?></td>
<td><?php echo $class_search; ?></td>
</tr>
<?php
$i++;
} ?>
</tbody>
</table>
I'm struggling with creating a html table from php array, while applying different css classes on different rows according to the array.
I have this code in data.php:
<?php
$data = array(
array('Title 1'=>'text11', 'Title 2'=>'text12'),
array('Title 1'=>'text21', 'Title 2'=>'text22'),
array('Title 1'=>'text31', 'Title 2'=>'text32'),
array('Title 1'=>'text41', 'Title 2'=>'text42', 'special'=>'style1'),
array('Title 1'=>'text51', 'Title 2'=>'text52', 'special'=>'style2'),
);
?>
I want to create a html table from this array, and if the array contains 'special'=>'style', it would set that style to that particular row. This is my code so far:
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if ($row == 'class1') {
$class='class="style1"';
} elseif ($row == 'class1') {
$class='class="style2"';
} else {
$class='';
}?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
And this is the desired output:
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>text11</td><td>text12</td>
</tr>
<tr>
<td>text21</td><td>text22</td>
</tr>
<tr>
<td>text31</td><td>text32</td>
</tr>
<tr class="style1">
<td>text41</td><td>text42</td>
</tr>
<tr class="style2">
<td>text51</td><td>text52</td>
</tr>
</tbody>
</table>
Your problem is this line:
<?php foreach ($data as $key=>$row):
You're forgetting that you're looping over a multidimensional array (i.e. an array of arrays) so that $row is an array here. On the next line:
if ($row == 'class1')
You're looking for a comparison between a string and $row which is an array. This will never work! As Daniel points out in his answer, you need to look at the contents of the array.
Don't forget you'll need to remove the special element from the array before displaying it. Personally, I'd condense the code a bit, though mixing PHP and HTML like this is never a good idea.
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): $class = $row["special"] ?? ""; unset($row["special"]);?>
<tr class="<?=$class?>">
<td><?=implode("</td><td>", $row)?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I'm not sure I understood everything but you can try this
<?php foreach ($data as $key => $row):
$class = '';
if(isset($row['special'])){
$class = 'class="'.$row['special'].'"';
unset($row['special']);
}
?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
If I understand your question correctly, this should do the job.
The result is as you desire, see: Online PHP shell
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if (isset($row["special"])) {
$class = " class='" . $row["special"]. "'";
unset($row["special"]);
} else {
$class='';
}?>
<tr<?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
i am successfully fetching data from model named user_model to my controller named as login_controller. as shown below
in user_model:
function fetchData()
{
$this->db->select('DeviceName, DeviceType,RegistrationDateTime,LastUpdateDateTime,LastPushNotificationSent');
$query = $this->db->get('userinfo');
$res=$query->result();
if($res)
{
return $res;
}
else
{
return NULL;
}
}
in my controller i have:
function displayDatabase()
{
$data['tableInfo']=$this->user_model->fetchData();
$this->load->view('adminPanel_view',$data);
}
where in adminPanel_view i am doing this:
<table class="table table-striped table-bordered table-hover table-full- width" id="sample_1">
<thead>
<tr>
<th>Serial No.</th>
<th class="hidden-xs">Device Name</th>
<th>Device Type</th>
<th>RegistrationDateTime </th>
<th>LastUpdateTime</th>
<th>LastPushNotificationSent</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php //print_r ($tableInfo);exit;?>
<?php foreach($tableInfo as $r): ?>
<tr>
<?php echo $r->DeviceName ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
but i am unable to show the data in table format. What am i doing wrong here.The data is displayed like this
Please try this in your view file...
<table class="table table-striped table-bordered table-hover table-full- width" id="sample_1">
<thead>
<tr>
<th>Serial No.</th>
<th class="hidden-xs">Device Name</th>
<th>Device Type</th>
<th>RegistrationDateTime </th>
<th>LastUpdateTime</th>
<th>LastPushNotificationSent</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php //print_r ($tableInfo);exit;?>
<?php foreach($tableInfo as $r): ?>
<tr>
<td><?php echo $r->no ?></td>
<td><?php echo $r->DeviceName ?></td>
<td><?php echo $r->DeviceType ?></td>
<td><?php echo $r->RegistrationDateTime ?></td>
<td><?php echo $r->LastUpdateTime ?></td>
<td><?php echo $r->LastPushNotificationSent ?></td>
<td><?php echo $r->Actions ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Hope this will help you.