display array parsed from ini as html table? - php

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>

Related

Put text between autofilled PHP-Table

I'm currently working on a table that contains 'Name', 'Info', 'Price' and 'Duration'. However the PHP-Script is connected to a database.
I want to autofill the tablecells: 'Name', 'Price', 'Duration' via the Database by using PHP and SQL and that works perfectly fine for me. Though, I want to customize the content that's in the individual Info cells (e.g. Readmore jQuery and redirect to other pages).
I tried a little bit with using tags inside the Database and other weird stuff which, obviously, didn't work.
Is there a more elegant way of solving my problem than setting up a complete normal table without PHP/SQL, where I'd have to put in every bit of data about Name,Price and Duration manually?
<table class="table table-bordered table-hover tablesorter row sortable">
<thead>
<tr>
<th class="header">Name</th>
<th class="hidden-xs">Info</th>
<th>Price (in Euro)</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
<?php
//Connect to Database
$db=mysql_connect ("xxxx", "xxxx", "xxxx") or die ('Oops! Da hat wohl' . mysqli_error(). 'Mist gebaut');
//Choose Database
$mydb=mysql_select_db("massageke_de");
//Query Database
$sql="SELECT * FROM Angebote";
//-run the query against the mysql query function
$result=mysql_query($sql);
//Show Results
while($row=mysql_fetch_array($result)){
//Start table ?>
<tr>
<td class="col-sm-2"><?php echo $Name =$row['Name'] ; ?></td>
<!--In this <td>-tag I want to put long textes with links-->
<td class="hidden-xs col-sm-5">echo $Name =$row['Info'];?<</td>
<td class="col-sm-2"><?php echo $Preis =$row['Preis']; ?></td>
<td ckass="col-sm-1"><?php echo $Dauer =$row['Dauer']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Thanks in advance for helping.
Don't bother asking me additional questions.
P.S.: I hope you can help, without needing the CSS of Bootstrap, that I used.
P.P.S.:I know that my PHP-Script is not protected against PHP-Injection
(but I want to and will learn how to secure it)
Edit: As Jester asked for it. I made it quickly with photoshop because I think an image can express much better, what I want to achieve, than my poorly coding-skills.
Get to the image
Seems to me like the easiest way would be to just edit the info column in the database for each? If you want to do it in php i'd suggest making an array using the names (ids would be better but it seems you don't have access to those?) as keys:
$info['Aroma Teilkoerper'] = "Text about aroma teilkoerper";
$info['Aroma Ganzkoerper'] = "Text about aroma ganzkoerper";
and so on until you have all. Then in the loop:
//Show Results
while($row=mysql_fetch_array($result)){
//Start table ?>
<tr>
<td class="col-sm-2"><?php echo $Name =$row['Name'] ; ?></td>
<!--In this <td>-tag I want to put long textes with links-->
<td class="hidden-xs col-sm-5">echo $Name =$info[$row['Name']];?></td>
<td class="col-sm-2"><?php echo $Preis =$row['Preis']; ?></td>
<td ckass="col-sm-1"><?php echo $Dauer =$row['Dauer']; ?></td>
</tr>
<?php } ?>
Hoping that is a workable solution for you? (also you had a syntax error in your closing php tag.
echo $Name =$row['Info'];?< // <----- should be ?> of course

parse_ini_string show only certain results

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;
}

create dynamic reports using core php and mysql

Currently we are doing a project to manage projects and our client need a dynamic reports which he can get Time Sheet Project wise.
Let me explain the situation.
3 database tables.
Project table, user table and task table.
This is the sample reports format with a sample data, but structure is the same.
Multiple users can work on a single project and user can work on multiple projects at the same time.
This is what i have tried to up to now.
<?php
/////get projects which are not finished/////
$sql_project = "SELECT * FROM project where P_Status!='finished'";
$result_project = mysql_query($sql_project) or die("Error in query: $sql_project.".mysql_error());
////////get users from the users table
$sql_getUsers="select * from tbl_user where status='Active' order by U_Name";
$result_getUsers=mysql_query($sql_getUsers);
?>
<tr>
<td>Project</td>
<td>Client</td>
<td>Estimated Cost</td>
<td >Total spent to date </td>
<?php
while($row_getUsers=mysql_fetch_array($result_getUsers))
{
?>
<td ><?php echo $row_getUsers['U_Name']; ?></td>
<?php
}
?>
</tr>
<?php
while($row_project=mysql_fetch_array($result_project))
{
$individualProject=trim($row_project['P_Title']);
$sql_tasks = "Select SUM(Time)as totalTime from tbl_txn where project like '%$individualProject%' and Date<='$endDate'";
$result_sql = mysql_query($sql_tasks) or die("Error in query: $sql_file_no.".mysql_error());
?>
<?php
while($row_sql_tasks=mysql_fetch_array($result_sql))
{
?>
<tr>
<td><?php echo $row_project['P_Title']; ?></td>
<td ><?php echo $row_project['P_Client']; ?></td>
<td ><?php echo $row_project['estimatedCost']; ?></td>
<td ><?php echo round($row_sql_tasks['totalTime']); ?></td>
<td><?php echo round($row_getIndiUserTime['Time']); ?></td>
<?php
}
}
?>
I have managed to display the users, but i have no idea how to display users have work on each project?.
on project A, how much time User 1 has spent and user 2 like wise.
hope i have clearly mentioned about the problem.
can anyone help me on this please?
Thanks in advance
The idea to do this is to join a table in mysql.
Here is a resource you can learn how to use mysql join table and show the data accordingly. Hope this will help you.

Separating PHP code from HTML output

Very often I have heard people suggesting (and I have done it myself too a few times) to keep things separate: PHP code here, HTML there, external CSS, external JS and so on and so on.
Aside from the obvious readibility and maintenance advantages of doing this are there other strong advantages (e.g. in terms of server load or page processing time) in doing it?
As a trivial example, say we want to implement a table containing some products we read from a DB.
The output we want would be something like
<div class="description">This table lists all our products</div>
<table class="products">
<tr>
<th>Name</th>
<th>Available</th>
<th>Price</th>
</tr>
<tr>
<td>Prod 1</td>
<td>Yes</td>
<td>$100</td>
</tr>
...
...
</table>
<div class="info">Some generic info on the products here</div>
So here we have some static output (the 2 div elements and the table header) and some dynamic output (the actual table content).
We could leave all the static things out of PHP tags and try to keep PHP only where needed
<div class="description">This table lists all our products</div>
<table class="products">
<tr>
<th>Name</th>
<th>Available</th>
<th>Price</th>
</tr>
<?
for ($p=0; $p<count($products); $p++)
{
echo '<tr>';
echo '<td>'.$products[$p]["name"].'</td>';
echo '<td>'.$products[$p]["availability"].'</td>';
echo '<td>'.$products[$p]["price"].'</td>';
echo '</tr>';
}
?>
</table>
<div>.....</div>
On the other hand we could embed everything in PHP
<?
echo '<div class="description">This table lists all our products</div>';
echo '<table class="products"><tr><th>Name</th>'.
'<th>Available</th><th>Price</th></tr>';
for ($p=0; $p<count($products); $p++)
{
echo '<tr>';
echo '<td>'.$products[$p]["name"].'</td>';
echo '<td>'.$products[$p]["availability"].'</td>';
echo '<td>'.$products[$p]["price"].'</td>';
echo '</tr>';
}
echo '</table>';
echo '<div>.....</div>';
What are the reasons to choose one over the other?
The alternative syntax for control structures seems to be more readable to me:
<div class="description">This table lists all our products</div>
<table class="products">
<tr>
<th>Name</th>
<th>Available</th>
<th>Price</th>
</tr>
<?php foreach($products as $p): ?>
<tr>
<td><?php echo $p["name"]; ?></td>
<td><?php echo $p["availability"]; ?></td>
<td><?php echo $p["price"]; ?></td>
</tr>
<?php endforeach; ?>
</table>
<div class="info"><?php echo $info; ?></div>
If its just a piece of code for you to play with, it doesn't really matter at all.
But if an application grows more and more complex (and more people work in it), it will usually come to a point where it is vital to separate the view layer (here: HTML) from the code layer (here: PHP) - so you can assign designers to play around with the output and coders to play around with the functionality behind it.
This ain't a php-only topic, this is very general. Architectural models like MVC are based on similar theories.
I think that it is a very compact string <?= $var ?>
I do not suggest to use short sytax of php. Sometimes it can be problem to move code from one server to another.
Reason you need to do so is time. Nice code is simple to support and upgrade. In some cases it is performance issue also, but not in your case.
Ideal example in your case is:
You have to files
index.php
products.php
File products.php contain
<?php
...
foreach($products as $product)
{
$productHTML[] = '<tr><td>' . $product["name"] . '</td></tr>';
}
$productHTML = implode("", productHTML);
?>
index.php:
<html>
...
<?php echo $productsHTML;?>
...
</html>
Ofcourse more advence developers use more hard constructions, we use functions, class, template idea and etc. But such way is enough for small project.

PHP Repeating Region and Checkboxes

--Edited for clarity.
Database:
tblModule, contains a list of modules that can be enabled or disabled.
tblData, contains a list of trusts and the modules they have enabled. This links to tblModule on tblData.M01 = tblModule.mod_key
The PHP page is accessed from an index page and passes a variable lstModTrust to this page, to limit the returned records from tblData for a single trust. tblData.trust_key
A query runs, qryModuleList which returns a list of all modules. This is used to generate a table of all available modules. Each row shows module name tblModules.mod_name, module code tblModules.mod_code and a checkbox.
qryModData will return a list of the modules that are enabled for a single trust, and the corresponding checkboxes need to be ticked in the table.
This page will then be used to enable and disable modules for a trust. If a module is unticked the entry will be deleted from tblData, if it is ticked an entry will be inserted, and if no change then no change in the DB.
At the moment I'm having trouble getting the checkboxes ticked correctly based on qryModData
Any thoughts anyone?
--Edited to include code--
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Module</td>
<td>Module Code</td>
<td> </td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_qryModuleList['mod_name']; ?></td>
<td><?php echo $row_qryModuleList['mod_code']; ?></td>
<td><input <?php if (!(strcmp($row_qryModData['M01'],$row_qryModuleList['mod_code']))) {echo "checked=\"checked\"";} ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
</tr>
<?php } while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)); ?>
</table>
Then there's two SQL queries, one that generates the list to build the table, and the second which I'm trying to use to set the boxes to ticked.
qryModuleList
SELECT *
FROM tblmodules
ORDER BY mod_name ASC
qryModData
SELECT *
FROM tbldata
WHERE trust_key = varTrust
varTrust is pulled from a URL variable.
Apologies for not including the code in the first place.
--Edit for new code.
<?php while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)) { ?>
<tr>
<td><?php echo $row_qryModuleList['mod_name']; ?></td>
<td><?php echo $row_qryModuleList['mod_code']; ?></td>
<td><input <?php if (strcmp($row_qryModData['M01'],$row_qryModuleList['mod_key']) != 0) {echo "checked=\"checked\"";} ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
</tr>
<?php } ; ?>
--Edited for new Code.
<tr class="tblHead">
<td>Module</td>
<td>Module Code</td>
<td>Enabled\Disabled</td>
</tr>
<?php
$mod_data = array();
while ($row_qryModData = mysql_fetch_assoc($qryModData))
array_push($mod_data, $row_qryModData['M01']);
$currentRow = 0;
while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)) {
?>
<tr bgcolor="<?php echo($currentRow++ % 2)?"#CCFFFF":"#FFCCFF";?>">
<td><?php echo $row_qryModuleList['mod_name']; ?></td>
<td><?php echo $row_qryModuleList['mod_code']; ?></td>
<td><input <?php if (false !== (array_search($mod_data, $row_qryModuleList['mod_key']))) echo "checked=\"checked\""; ?> name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
</tr>
<?php } ; ?>
I think I'm understanding this now. What you need to do is place all of the allowed modules into an array and use the array_search() function to find it.
Example:
$mod_data = array();
while ($row_qryModData = mysql_fetch_assoc($qryModData)) array_push($mod_data, $row_qryModData['M01']);
That will get all the available modules into one array.
Next, while you cycle through the 'ModuleList' query, use the array_search() method to try and find the 'ModKey' variable in it. If you do, check the box. If not, do nothing.
Example:
<td><input <? if (false !== (array_search($mod_data, $row_qryModuleList['mod_code'])) echo "checked=\"checked\" "; ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
(The reason I use a "!==" is because the function can return false or something that might equal false and might not. Read the page I've linked above for more)

Categories