Array to HTML Table - php

I try to insert data from my php array into an html table. The code for this looks like
echo "<table style='border: 1px solid black'><tbody>";
foreach(['country','counter'] as $attribute){
echo "<tr><td>".$attribute."</td>";
foreach($analysis_data as $row){
echo "<td style='border: 1px solid black'>".$row[$attribute]."<td>";
}
echo '</tr>';
When the code is executed it looks like
I want the table to be vertical and not horizontal like this. What do I need to change in my code?

Interchange your two loops.
echo "<table style='border: 1px solid black'><tbody>";
echo "<tr><th>country</th><th>counter</th></tr>";
for ($analysis_data as $row) {
echo '<tr>';
foreach (['country','counter'] as $attribute){
echo "<td style='border: 1px solid black'>".$row[$attribute]."<td>";
}
echo '</tr>';
}

Related

Cant apply CSS style

Ive been trying to increase the font size for the TH tag via the css style sheet:
th {
color:#D5DDE5;
background:#1b1e24;
border-bottom:4px solid #9ea7af;
border-right: 1px solid #343a45;
font-size:23px;
font-weight: 100;
padding:24px;
text-align:left;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
vertical-align:middle;
}
for the following code:
<?php
include ('connect-mysql.php');
$sqlget="SELECT * FROM data ORDER BY score desc LIMIT 10";
$sqldata= mysqli_query($conn, $sqlget);
echo "<table width='1500'>";
echo "<tr>";
echo "<th>Position</th>";
echo "<th>First Name</th>";
echo "<th>Last Name</th>";
echo "<th>Score</th>";
echo "</tr>";
$counter = 1;
while($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) {
echo "<tr><td width='5%'>";
echo "<b>".$counter."</b>";
echo "</td><td>";
echo "<div style='font-size:40px'>";
echo "<b>".$row['firstName']."</b>";
echo "</div>";
echo "</td><td>";
echo "<div style='font-size:40px'>";
echo "<b>".$row['lastName']."</b>";
echo "</div>";
echo "</td><td width='20%'>";
echo "<b>".$row['score']."</b>";
echo "</td></tr>";
$counter++;
}
echo "</table>";
?>
As you can see i have been using inline css styles as the external style sheet seems to have jammed, i would like to increase the font size of the TH tags but there is no response when changing 'font-size:23px'
What am i doing wrong ?
Try to change font-size in the browser development panel.
It will show you if there are some other css instructions that overwrites yours (or any other reason).
Thanks for this I managed to get it to work using kunals help.
echo "<th style='font-size:2.0em'>Position</th>";
using "" instead of ''inside the th tag causes php errors for me.
using px caused formatting issues so i used em
I am still wondering why my CSS style sheet isnt working :/
Make it like this, and you're done...
echo "<table width='1500'>";
echo "<tr>";
echo "<th style="font-size:23px;">Position</th>";
echo "<th style="font-size:23px;">First Name</th>";
echo "<th style="font-size:23px;">Last Name</th>";
echo "<th style="font-size:23px;">Score</th>";
echo "</tr>";
add inline css to th tags...

MySQL(i?) Filter on an existing query

I want to create a filter which filters my ouput from the database. I have no idea how to create this. I have searched some other topics but they were not so helpful, hopefully someone can help me out.
I have the following code which retrieves the information from my database and ouputs it into a table. (please ignore the table set-up and such, still need to clean the nesting and stuff..)
Code (retrieve.php):
<?php
// Connect to database server
mysql_connect("xx", "xx", "xx") or die (mysql_error ());
// Select database
mysql_select_db("xx") or die(mysql_error());
// SQL query
$strSQL = "SELECT * FROM informatie ORDER BY id DESC;";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
//OUDE LOOP SPACE
// Write the value of the column FirstName (which is now in the array $row)
echo "<table width='100%' border='0' cellpadding='10' cellspacing='0'><tr style='background-color:#f04d44; color:#ffffff;'>";
echo "<td style='border-right:1px solid #ff9a88;'><strong>Klant</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Datum</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Eventviewer Nagekeken</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Eventviewer Opmerkingen</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Services Nagekeken</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Services Opmerkingen</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Backup Nagekeken</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Backup Opmerkingen</strong></td><td><strong>Check Gedaan Door</strong></td>";
echo "</tr>";
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {
echo "<tr style='background-color:#ffffff;'>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['klant'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['datum'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['eventviewer_nagekeken'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['eventviewer_opmerkingen'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['services_nagekeken'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['services_opmerkingen'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['backup_nagekeken'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['backup_opmerkingen'];
echo "</td>";
echo "<td style='border-bottom:1px solid #cccccc;'>";
echo $row['check_door'];
echo "</td>";
}
echo "</tr></table>";
// Close the database connection
mysql_close();
?>
Thanks for helping me out!
For filtering, most of the time you can use your database.
Right now your SQL statement is like this:
$strSQL = "SELECT * FROM informatie ORDER BY id DESC;";
To filter on information from the table informatie, you should alter the SQL statment, for example:
$strSQL = "SELECT * FROM informatie WHERE `eventviewer_nagekeken` = 1 ORDER BY id DESC;";
This will return all rows where eventviewer_nagekeken equals 1. This way you will be able to create various outputs from the pool of data you have in the informatie table.
For documentation and examples, see the docs.
you can use where clause generated dynamically as bellow
$where = ' 1=1 ';
if(isset($_REQUEST['param1']))
{
$where .= " and db_field1 = ".mysql_real_escape_string($_REQUEST['param1']);
}
if(isset($_REQUEST['param2']))
{
$where .= " and db_field2 = ".mysql_real_escape_string($_REQUEST['param2']);
}
$strSQL = "SELECT * FROM informatie ".$where." ORDER BY id DESC;";
here mysql_real_escape_string will avoid mysql injection.
Hope this helps you.

ID not valid on Edit button code

I have a code that displays the data that is stored in a database. I want to make an edit button or link that allows me to edit the data (I have a table that displays the data in columns and rows).
Snippet of my edit code
// once saved, redirect back to the view page
header("Location: insertchart.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!123';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['newId']) && is_numeric($_GET['newId']) && $_GET['newId'] > 0)
{
// query db
$newId = $_GET['newId'];
$result = mysql_query("SELECT * FROM charts WHERE newId=$newId")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$charts_date = $row['charts_date'];
$charts_retrace = $row['charts_retrace'];
$charts_start_of_swing_trade = $row['charts_start_of_swing_trade'];
$charts_end_of_swing_trade = $row['charts_end_of_swing_trade'];
$charts_bullflag = $row['charts_bullflag'];
$charts_bearflag = $row['charts_bearflag'];
$charts_ema_crossover = $row['charts_ema_crossover'];
$charts_trading_instrument = $row['charts_trading_instrument'];
// show form
renderForm($newId, $charts_date, $charts_retrace, $charts_start_of_swing_trade, $charts_end_of_swing_trade, $charts_bullflag, $charts_bearflag, $charts_ema_crossover, $charts_trading_instrument, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!345';
}
}
?>
I get Error!345 when I click on my Edit button so I know it is quering the database fine but I have no idea why it is telling me that there is an error with my ID.
As requested, here is what my table code looks like that displays the Edit link:
$result = $conn->query($sql);
if($result && $result->num_rows > 0) {
// output data of each row
echo "<h2>What is currently inside the database?</h2><br><br>
<table style='border: solid #000000 1px;border-collapse:collapse;'>
<tr>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Chart</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Date</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Retrace</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Start of Swing Trade</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>End of Swing Trade</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Bull flag</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Bear flag</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>EMA Crossover</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Trading Instrument</u></strong></td>
</tr>";
while ($row=mysqli_fetch_array($result)) {
echo "<tr><td style='border: solid #000000 1px;'><img src=". $row["charts_URL"]. " width='200px'></td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_date"]. "<br>"; echo "<a href='edit.php?id=" . $row['newId'] . "'>Edit</a></td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_retrace"]. "</td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_start_of_swing_trade"]. "</td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_end_of_swing_trade"]. "</td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_bullflag"]. "</td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_bearflag"]. "</td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_ema_crossover"]. "</td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_trading_instrument"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
The Edit code is underneath the charts_date cell.

convert Div tag of my page into pdf in php while data is coming from databse

I want to convert the valve which are inside of the DIV tag to PDF file
<div id="container2">
<?php
$con=mysql_connect('localhost','root','');
mysql_select_db('account',$con);
$query1="Select * from e_bills";
$result=mysql_query($query1);
echo "<table width='1000' border='5' cellpadding='2' id='bill'>
<tr>
<th style='border:1px solid black'><input type='checkbox'></th>
<th style='border:1px solid black'>BillNo</th>
<th style='border:1px solid black'>Amount</th>
<th style='border:1px solid black'>VendorName</th>
<th style='border:1px solid black'>Date</th>
<th style='border:1px solid black'>Paid</th>
<th style='border:1px solid black'>Pending</th>
<th style='border:1px solid black'>Action</th</tr>";
//$res=mysql_fetch_array($result);
//print_r($res);
while($res=mysql_fetch_array($result))
{
$pending=$res['bi_total']-$res['bi_paid'];
echo "<tr> <td style='border:1px solid black';>";
echo "<input type='checkbox' class='ads_Checkbox' id='checkbox.$res[bi_billnumber]' value='$res[bi_billnumber]'>";
echo " </td>";
echo "<td style='border:1px solid black';>";
echo "B_".$res['bi_billnumber'];
echo "</td>";
echo "<td style='border:1px solid black';>";
echo $res['bi_total'];
echo "</td> ";
echo "<td style='border:1px solid black';><a style='color:red;text-decoration:underline;cursor:pointer;'onclick='getdata1($res[bi_billnumber])'>";
echo $res['bi_vendername'];
echo "</a></td>";
echo "<td style='border:1px solid black';>";
echo $res['bi_date'];
echo "</td>";
echo "<td style='border:1px solid black';>";
echo $res['bi_paid'];
echo "</td>";
echo "<td style='border:1px solid black';>";
echo "$pending";
echo "</td>";
echo "<td style='border:1px solid black';>";
echo "<input type='button' name='button' value='Edit' onclick='getdata($res[bi_billnumber])'>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</form>
</div>
I have Used
<?php
include("/MPDF53/MPDF53/mpdf.php");
$mpdf=new mPDF('c','A4','','' , 0 , 0 , 0 , 0 , 0 , 0);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list
$mpdf->WriteHTML(file_get_contents('billdisplay.php'));
$mpdf->Output();
?>
But useful for only from static values not for the data which is coming from the database.
You can use the dompdf for converting your html to pdf. It is so simple and it also take php code . Dompdf Example

Combining several comments into one string

How could I combine all of the comments that appear in $row["comment"] below into one giant string variable called $commentstring?
$sqlStr = "SELECT comment.comment, comment.datecommented, comment.commentid, comment.points, login.username
FROM comment
LEFT JOIN login ON comment.loginid=login.loginid
WHERE submissionid=$submissionid
ORDER BY comment.points DESC
LIMIT 100";
$tzFrom1 = new DateTimeZone('America/New_York');
$tzTo1 = new DateTimeZone('America/Phoenix');
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"commentecho\">";
$count = 1;
while ($row = mysql_fetch_array($result)) {
$dt1 = new DateTime($row["datecommented"], $tzFrom1);
$dt1->setTimezone($tzTo1);
echo '<tr>';
echo '<td style="border-left:3px solid #DE2A00; background-color: #DE2A00; border-top:3px solid #DE2A00;" class="commentname2user">'.$row["username"].'</td>';
echo '<td style="border-bottom:3px solid #DE2A00; border-top:3px solid #DE2A00; border-right:3px solid #DE2A00;" rowspan="4" class="commentname1" id="comment-' . $row["commentid"] . '">'.stripslashes($row["comment"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td style="border-left:3px solid #DE2A00; background-color: #DE2A00;" class="commentname2">'.$dt1->format('F j, Y').'</td>';
echo '</tr>';
echo '<tr style="border-left:3px solid #DE2A00; background-color: #DE2A00; border-bottom:0px solid #DE2A00;">';
echo '<td style="border-left:3px solid #DE2A00;" class="commentname2"></td>';
echo '</tr>';
echo '<tr>';
echo '<td style="border-left:3px solid #DE2A00; background-color: #DE2A00; border-bottom:3px solid #DE2A00;" class="commentname2user"><span class="">'.number_format($row["points"]).'<span></td>';
echo '</tr>';
echo '<tr>';
echo '<td style="border-bottom:0px solid #DE2A00; border-right:0px solid #DE2A00;" class="commentname2a"></td>';
echo '</tr>';
}
echo "</table>";
Declare $commentstring = ""; before the while-loop
in the while loop:
$commentstring .= $row["comment"];
If you put this before your while loop:
$commentstring = '';
and then inside the while loop you add onto it:
$commentstring .= $row['comment'];
Or together with a new line:
$commentstring .= $row['comment'] . '<br />';

Categories