I have written some code that parses information from a .ini file into a table.
<?php
$datas = parse_ini_string(file_get_contents( 'https://drive.google.com/uc?export=download&id=pathtoinihere' ), true);
?>
<table border="1" cellspacing="0" cellpadding="5" width="100%">
<tbody class="points-cell">
<tr>
<td class="points-header"><b>Name</b></td>
<td class="points-header"><b>Points</b></td>
</tr>
<?php
foreach( $datas as $section => $data ) {
?>
<tr>
<td class="points-section"><?php echo htmlspecialchars( $section ); ?></td>
<td><?php echo htmlspecialchars( $data["Points"] ); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
Currently this exports the following:
Name | Points
name1 | 50
unwanted | 7377
name2 | 22
I want to make it so as the .ini file is parsed, the name 'unwanted' and the points related are not parsed.
The way I figure this should be done, and please correct me if I am wrong:
foreach( $datas as $section => $data ) {
if($section=="unwanted"){
$section=="";
}
This is where I get lost. Any guidance helps more than you know.
You can use the continue keyword in PHP for your unwanted items:
foreach( $datas as $section => $data ) {
if($section=="unwanted") continue;
}
Related
I have simulated the mysql result from an old SO question as the following array:
<?php
$arr = array(
array(
'title'=>'Test',
'name'=>'ABC',
'cat_desc'=>'ABC_DESC',
'parent'=>0,
'parent_menu'=>1
),
array(
'title'=>'Test2',
'name'=>'DEF',
'cat_desc'=>'DEF_DESC',
'parent'=>0,
'parent_menu'=>2
),
array(
'title'=>'Test2',
'name'=>'GHI',
'cat_desc'=>'GHI_DESC',
'parent'=>1,
'parent_menu'=>0
),
array(
'title'=>null,
'name'=>'JKL',
'cat_desc'=>'JKL_DESC',
'parent'=>2,
'parent_menu'=>0
)
);
//print_r($arr);
?>
Now I wonder if I could print the result in this format:
<table>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<tr>
<td> Menu Title Test </td>
<td> Main Category ABC</td>
</tr>
<tr>
<td>GHI</td>
<td>GHI_DESC</td>
</tr>
<tr>
<td> Menu Title Test2 </td>
<td> Main Category DEF</td>
</tr>
<tr>
<td>JKL</td>
<td>JKL_DESC</td>
</tr>
</table>
I am trying the following php to print but it could not give the expected result:
<table>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<?php
$r = 0;
while(list($key, $val)=each($arr)) {
if($val['parent']==0):
if($r % 1==0): ?>
<tr>
<td> Menu Title <?php echo $val['title'];?> </td>
<td> Main Category <?php echo $val['name'];?></td>
</tr>
<?php endif;
endif;
if($val['parent']!=0){ ?>
<tr>
<td><?php echo $val['name'];?></td>
<td><?php echo $val['cat_desc'];?></td>
</tr>
<?php
}
$r++;
} ?>
</table>
Your help and suggestion is very much welcome.
I find it difficult to figure out what you exactly want from your array structure. But I noticed that array element 0 is paired with element 2, and element 1 is paired with element 3. Therefore my code would be like following. It will give you exactly the table that you wanted.
print "<table border=1>";
$numArray = count($arr) / 2;
for($i=0;$i<$numArray;$i++) {
$element = $arr[$i];
print "<tr><td>Menu Title {$element['title']}</td><td>Main Category {$element['name']}</td></tr>";
$element = $arr[$i+2];
print "<tr><td>{$element['name']}</td><td>{$element['cat_desc']} </td></tr>";
}
print "</table>";
You can use this where I commented what to do and why
<table>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<?php
// Store the array into temporary array
$temp_array = $arr;
foreach ($arr as $akey => $sArr)
{
// Check that prrent is 0
if ( $sArr['parent'] ==0 )
{
// Store parent_menu id
$parent_menu = $sArr['parent_menu'];
?>
<tr>
<td> Menu Title <?php echo $sArr['title'];?> </td>
<td> Main Category <?php echo $sArr['name'];?></td>
</tr>
<?php
// Unset or remove current index from array
unset($temp_array[$akey]);
// Iterate all rows
foreach ($temp_array as $skey => $aval)
{
// Check that it has parent for the current menu
if ( $aval['parent'] == $parent_menu )
{
?>
<tr>
<td><?php echo $aval['name'];?></td>
<td><?php echo $aval['cat_desc'];?></td>
</tr>
<?php
}
}
// End of foreach
}
// End of if
}
// End of foreach
?>
</table>
I find that data from a relational DB doesn't really lend itself to direct use and requires some massaging to lower the code complexity. So, if your aim is to make a dynamic menu with submenus, I would suggest refactoring the data structure into more of a tree structure and then looping over each level.
A better data structure would be:
$menus = array(
1 => array( // original array item index 0
'title'=>'Test',
'name'=>'ABC',
'cat_desc'=>'ABC_DESC',
'parent'=>0,
'parent_menu'=>1
'children'=> array( // new array containing submenus
array( // original array item index 2
'title'=>'Test2',
'name'=>'GHI',
'cat_desc'=>'GHI_DESC',
'parent'=>1,
'parent_menu'=>0
),
),
),
2 => array( // original array item index 1
'title'=>'Test2',
'name'=>'DEF',
'cat_desc'=>'DEF_DESC',
'parent'=>0,
'parent_menu'=>2
'children'=> array( // array containing original item index 2
array(
'title'=>null,
'name'=>'JKL',
'cat_desc'=>'JKL_DESC',
'parent'=>2,
'parent_menu'=>0
),
),
),
);
Rather than just entering the data in this more covenient form, it is good practice to convert it. So, to get from the original data to this structure you can use a simple single pass foreach loop:
$menus = [];
foreach($arr as $item) {
if($item['parent'] == 0) {
$item['children'] = array();
$menus[$item['parent_menu']] = $item;
}
else {
$menus[$item['parent']]['children'][] = $item;
}
}
Once you have the data in a nice convenient form you can use simple nested foreach loops to iterate it and output the table in a clear manner like this:
<table>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<?php foreach($menus as $menu) { ?>
<tr>
<td> Menu Title <?php echo $menu['title'];?> </td>
<td> Main Category <?php echo $menu['name'];?></td>
</tr>
<?php foreach($menu['children'] as $child) { ?>
<tr>
<td><?php echo $child['name'];?></td>
<td><?php echo $child['cat_desc'];?></td>
</tr>
<?php } } ?>
</table>
From what I see, I believe $r % 2 makes a little more sense than $r % 1
In case $r is an integer, $r % 1 will always return a zero. Looks like in your case you use this to split logic for odd and even numbers, which makes sense for your data set.
Make sure the data set doesn't change. Maybe it'd be better use a more reliable approach. Adding indexes to the data set wouldn't harm either :)
I am really struggling attempting to scrape a table either via XPath or any sort of 'getElement' method. I have searched around and attempted various different approaches to solve my problem below but have come up short and really appreciate any help.
First, the HTML portion I am trying to scrape is the 2nd table on the document and looks like:
<table class="table2" border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr><th colspan="8" align="left">Status Information</th></tr>
<tr><th align="left">Status</th><th align="left">Type</th><th align="left">Address</th><th align="left">LP</th><th align="left">Agent Info</th><th align="left">Agent Email</th><th align="left">Phone</th><th align="center">Email Tmplt</th></tr>
<tr></tr>
<tr>
<td align="left">Active</td>
<td align="left">Resale</td>
<td align="center">*Property Address*</td>
<td align="right">*Price*</td>
<td align="center">*Agent Info*</td>
<td align="center">*Agent Email*</td>
<td align="center">*Agent Phone*</td>
<td align="center"> </td>
</tr>
<tr>
<td align="left">Active</td>
<td align="left">Resale</td>
<td align="center">*Property Address*</td>
<td align="right">*Price*</td>
<td align="center">*Agent Info*</td>
<td align="center">*Agent Email*</td>
<td align="center">*Agent Phone*</td>
<td align="center"> </td>
</tr>
...etc
With additional trs continuing containing 8 tds with the same information as detailed above.
What I need to do is iterate through the trs and internal tds to pick up each piece of information (inside the td) for each entry (inside of the tr).
Here is the code I have been struggling with:
<?php
$payload = array(
'http'=>array(
'method'=>"POST",
'content'=>'key=value'
)
);
stream_context_set_default($payload);
$dom = new DOMDocument();
libxml_use_internal_errors(TRUE);
$dom->loadHTMLFile('website-scraping-from.com');
libxml_clear_errors();
foreach ($dom->getElementsByTagName('tr') as $row){
foreach($dom->$row->getElementsByTagName('td') as $node){
echo $node->textContent . "<br/>";
}
}
?>
This code is not returning nearly what I need and I am having a lot of trouble trying to figure out how to fix it, perhaps XPath is a better route to go to find the table / information I need, but I have come up empty with that method as well. Any information is much appreciated.
If it matters, my end goal is to be able to take the table data and dump it into a database if the first td has a value of "Active".
Can this be of any help?
$table = $dom->getElementsByTagName('table')->item(1);
foreach ($table->getElementsByTagName('tr') as $row){
$cells = $row->getElementsByTagName('td');
if ( $cells->item(0)->nodeValue == 'Active' ) {
foreach($cells as $node){
echo $node->nodeValue . "<br/>";
}
}
}
This will fetch the second table, and display the contents of the rows starting with a first cell "Active".
Edit: Here is a more extensive help:
$arr = array();
$table = $dom->getElementsByTagName('table')->item(1);
foreach ($table->getElementsByTagName('tr') as $row){
$cells = $row->getElementsByTagName('td');
if ( $cells->item(0)->nodeValue == 'Active' ) {
$obj = new stdClass;
$obj->type = $cells->item(1)->nodeValue;
$obj->address = $cells->item(2)->nodeValue;
$obj->price = $cells->item(3)->nodeValue;
$obj->agent = $cells->item(4)->nodeValue;
$obj->email = $cells->item(5)->nodeValue;
$obj->phone = $cells->item(6)->nodeValue;
array_push( $arr, $obj );
}
}
print_r( $arr );
I'm currently having issue explode to html table.
My Controller
public function idp_test()
{
$this->load->model('groups/groups_model');
$scholar_id = $this->session->userdata('scholar_id');
$groups = $this->groups_model->retrieve_groups_idp($this->scholar->level_id);
$content = array(
'groups' => $groups,
'scholar' => $this->scholar,
'page_title' => 'My individual development plan',
'page_view' => 'scholar/activities/idp_test',
'page_scripts_view' => 'scholar/inc/_choose_activities_scripts',
'sidebar_menu' => 'idp test'
);
$this->load->view("theme/base", $content);
}
My Model
public function retrieve_groups_idp($level_id)
{
$this->db->select("groups.*, levels.name as levelname");
$this->db->join('activities', 'groups.activity_ids = activities.activity_id' );
$this->db->join('levels', 'levels.level_id = groups.level_id', 'left' );
$this->db->join('activity_types', 'activities.activity_type_id = activity_types.activity_type_id', 'left' );
$this->db->where('groups.level_id', $level_id);
$this->db->group_by('groups_name');
$this->db->order_by('groups_name');
$qr = $this->db->get('groups');
return $qr->result();
}
public function retrieve_activity_desc($activity_ids)
{
$activity_desc_arr[] = explode(', ', $activity_ids);
foreach ($activity_desc_arr as $activity_id){
$this->db->select("activity_id as activityid, CONCAT(activities.name, ' - ', activity_types.name) AS description", FALSE);
$this->db->join('activity_types', 'activities.activity_type_id = activity_types.activity_type_id');
$this->db->where_in('activities.activity_id', $activity_id);
$qr = $this->db->get('activities');
return $qr->result();
}
}
My View
<?php foreach ($groups as $groups): ?>
<table class="table table-vcenter">
<thead>
<tr class="active">
<th><?php echo $groups->groups_name?></th> <!--Series Header -->
<th class="text-center"><small><strong>Module Type</strong></small></th>
<th class="text-center"><small><strong>Status</strong></small></th>
<th class="text-center"><small><strong>Action</strong></small></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php
$rows = $this->groups_model->retrieve_activity_desc($groups->activity_ids);
foreach ($rows as $row){
echo var_dump($row->description);
}
?></td>
<td class="text-center">belom lagi</td>
<td class="text-center">TO ATTEND</td>
<td class="text-center">View Session</td>
</tr>
</tbody>
</table>
<?php endforeach ?>
i can't post images, so below are some sample in view so far
SERIES 1 FOR FS | MODULE TYPES | STATUS | ACTION
Series 1 - FILSeries 1 - CBLSeries 1 - PEL | belom lagi | to attend |
and what i need to achieve is like below sample in view
SERIES 1 FOR FS | MODULE TYPES | STATUS | ACTION
Series 1 - FIL | belom lagi | to attend |
Series 1 - CBL | belom lagi | to attend |
Series 1 - PEL | belom lagi | to attend |
I'm newbie with php & codeigniter, sorry with my english.
Thanks
Think about what you have in your view code:
<tr>
<td><?php
foreach($rows as $row){
var_dump($row->description);
}
?>
</td>
<td>belom lagi</td>
<td>to attend</td>
</tr>
Of course this will all be printed into one table cell, how it could produce anything else, right?
What you need is something like this:
<?php
foreach($rows as $row){
echo "<tr>"; // you need a new row for every entry
echo "<td>".$row->description."</td>";
echo "<td>belom lagi</td>";
echo "<td>to attend</td>";
echo "</tr>";
}
?>
After a few studies, i figured it out, just want to share with everybody.
EDITED - Controller
public function idp_test()
{
$this->load->model('groups/groups_model');
$this->load->model('activities/activity_type_model');
$scholar_id = $this->session->userdata('scholar_id');
$groups = $this->groups_model->retrieve_groups_idp($this->scholar->level_id);
foreach ($groups as $group) {
$act_desc = $this->groups_model->retrieve_activity_desc($group->activity_ids);
$group->activity_ids = $act_desc;
}
EDITED - View
<!-- START TABLE HEADER -->
<?php foreach ($groups as $group): ?>
<table class="table table-vcenter">
<thead>
<tr class="active">
<th><?php echo $group->groups_name?></th> <!--Series Header -->
<th class="text-center"><small><strong>Module Type</strong></small></th>
<th class="text-center"><small><strong>Status</strong></small></th>
<th class="text-center"><small><strong>Action</strong></small></th>
</tr>
</thead>
<tbody>
<?php foreach ($group->activity_ids as $session): ?>
<tr>
<td><?php echo $session->description; ?></td>
<td class="text-center"><?php echo $session->modulename; ?></td>
<td class="text-center">STATUS</td>
<td class="text-center">View Session</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<?php endforeach ?>
<!-- END 1 -->
Come out as what i need.
Thank you stackoverflow.
On my website I have a table with the following code:
<table width="100%" id="tabloadwar" cellspacing="1" cellpadding="0" border="0">
<?php
$subdirs = array_map('intval', scandir("/war"));
$max = max($subdirs);
for($i=1;$i<=max;$i++){ ?>
<tr>
<td width="15%"><?php echo $i; ?></td>
<td width="85%">
<?php $percorso = file("dir1/content.txt"); $line = file($percorso); echo $line?></td></tr>
<?php } ?>
</table>
Using the code above, I want to create a table that has as much rows as the count of directories in my server. By the way it is not working because I don't see anything inside my table. An example of a row is this
<tr>
<td width="15%">1</td> // i value
<td width="85%">Italy</td> //text on the 1st line of my *.txt file
</tr>
Also, instead of that name I must put the content of a file called dir1/content.txt. It is not working too because I still don't see anything. Any help?
You must muse $max instead of max here for($i=1;$i<=max;$i++){
Try this code and see what happens..
<?php
if ( !file_exists( "/war" ) ) {
echo "'/war' not exists<br />";
}
if ( !file_exists( "dir1/content.txt" ) ) {
echo "'content.txt' not exists<br />";
}
?>
<table width="100%" id="tabloadwar" cellspacing="1" cellpadding="0" border="0">
<?php
$subdirs = array_map( "intval", scandir( "/war" ) );
$max = max( $subdirs );
for( $i = 1; $i <= $max; $i++ ) {
?>
<tr>
<td width="15%"><?php echo $i; ?></td>
<td width="85%">
<?php
$lines = file( "dir1/content.txt" );
echo implode( "<br />", $lines );
?>
</td>
</tr>
<?php
}
?>
</table>
You said ....rows as the count of directories in my server...
if you want to get the count of sub folders in folder you can use glob with GLOB_ONLYDIR flag
<?php
$dirs = glob( "path/to/dir/*", GLOB_ONLYDIR );
$total_dirs = count( $dirs );
?>
I'm trying to create a table of info about mods in a pack for a game server,
I can parse the INI but can't figure out how to place it into an html table properly.
the ini is:
[industrialcraft]
name = IndustrialCraft²
dev = Alblaka
website = http://www.industrial-craft.net
wiki = industrialcraft
[buildcraft]
name = BuildCraft
dev = SirSengir
website = http://www.mod-buildcraft.com/
wiki = buildcraft
I'm trying to get it into columns for the dev,website,etc. with headers at the top (name section doesn't need the header). and the wiki one is going to link to domain.com/wiki/bunchofstuff?id=insert_here
I want it to look sort of like this:
| Dev | Official Website | Wiki Page
IndustrialCraft | Alblaka | http://www.industrial-craft.net | wiki link
I can manage the layout myself, but I figured I should specify in case it affects how it needs to be coded.
I've been trying to figure it out myself but I'm still a bit of a noob with php..
Was that really hard?
<?php
$datas = parse_ini_file( $ini_file, true );
?>
<table border="1" cellspacing="0" cellpadding="5">
<tbody>
<?php
foreach( $datas as $data ) {
?>
<tr>
<td rowspan="2"><?php echo htmlspecialchars( $data["name"] ); ?></td>
<td>Dev</td>
<td>Official Website</td>
<td>Wiki Page</td>
</tr>
<tr>
<td><?php echo htmlspecialchars( $data["dev"] ); ?></td>
<td><?php echo htmlspecialchars( $data["website"] ); ?></td>
<td>
wiki link
</td>
</tr>
<?php
}
?>
</tbody>
</table>