This question already has answers here:
variable variables bad practice to use?
(2 answers)
Closed 9 months ago.
The following SQL PHP code shows count of country from database and displays it in a table.
Here is the code
foreach($dbh->query('SELECT country,COUNT(*)
FROM data_able
GROUP BY country') as $row) { $array = array();
echo "<tr>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['COUNT(*)'] . "</td>";
echo "</tr>";
}
Here is the result
Here is the result
I would like to use the data as $country1 and $count1.
For example $country1 will output "Andorra" and $country5 will be "India".
and $count5 should give "25" as result. How can I do that ?
Here is an example how to declare variables dynamicly:
$array = ['1', '2', '3', '4', '5'];
foreach ($array as $i) {
${'country' . $i} = $i;
}
echo $country2;
You just need to iterate through your query results with keeping some index on variable suffix (in your case a number).
// create a dynamic variables
$result = $dbh->query('SELECT country,COUNT(*) FROM data_able GROUP BY country');
$i = 1;
foreach ($result as $row) {
${'country' . $i} = $row['country'];
${'count' . $i} = $row['COUNT(*)'];
$i++;
}
// example how to use them
$i = 1;
foreach($result as $row) {
$array = array();
echo "<tr>";
echo "<td>" . ${'country' . $i} . "</td>";
echo "<td>" .${'count' . $i} . "</td>";
echo "</tr>";
$i++;
}
Related
trying to join to 2 xml files with the same value , but when i echo the result it separates the value .
foreach(glob("tarea/*.{assemble.xml,report.xml}", GLOB_BRACE) as $filename) {
$Keys =simplexml_load_file($filename);
foreach($Keys as $Key) {
$key1 = $Keys->ProductKeyID;
$pk = $Keys->ProductKey;
$key2 = $Keys->ProductKeyID;
$serial = $Keys->SerialNumber;
if ($key1 == $key2) {
echo "<tr>";
echo "<td>" .$pk."</td>";
echo "<td>" .$key1."</td>";
echo "<td>" .$key2."</td>";
echo "<td>" . $serial."</td>";
echo "</tr>";
break;
}
}
}
is there any way of putting something like
if key1 = key2 join the together
this is my result im hoping to put all in one line .
ProductKey ProductKeyID ProductKeyID SerialNumber
1234 3305492290985 3305492290985
3305492290985 3305492290985 23
I need to display:
/images/image1.jmp if the number is positive
or
/images/image2.jpg if the number is negative
or
/images/image3 if the number is 0.
$stmt = sqlsrv_query($conn,$sql);
echo "<table border='1'><tr><th>Offense</th><th>Previous Date Range</th><th>Current Date Range</th><th>Difference</th><th>Percentage Difference</><th>Up or down image in this column</></tr>";
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
echo "<td>" . $row['']. "</td>";
}
echo "</table>";
?>
I found this code and have tried different ways if incorporating it in the echo "<td>" . $row['']. "</td> but not having any luck.
I get the code but can not manipulate it to fit what it needs to do. I'm sure it's a simple solution. Just frustrated.
switch ($myNumber) {
case 0:
echo "Zero is not a valid value.";
break;
case $myNumber < 0:
echo "Negative numbers are not allowed.";
break;
default:
echo "Great! Ready to make calculations.";
break;
}
Thanks for the help guys. Got the answer that worked.
I think this is a more readable solution.
I use an array to hold the links and use a calculation to see if it is negative or positive.
$img = ["-1" => "/images/image2.jpg",
"0" => "/images/image3.jpg",
"1" => "/images/image1.jpg"];
$number = 0;
Echo ($number == 0 ? $img[$number] : $img[$number/abs($number)]);
https://3v4l.org/JstZl
If the number is positive the calculation will be 15/15 => 1.
If the number is negative -10/10 => -1.
Edit:
$stmt = sqlsrv_query($conn,$sql);
echo "<table border='1'><tr><th>Offense</th><th>Previous Date Range</th><th>Current Date Range</th><th>Difference</th><th>Percentage Difference</><th>Up or down image in this column</></tr>";
$img = ["-1" => "/images/image2.jpg",
"0" => "/images/image3.jpg",
"1" => "/images/image1.jpg"];
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
Echo '<td><img src="' . ($row['percentchange'] == 0 ? $img[$row['percentchange']] : $img[$row['percentchange']/abs($row['percentchange'])]) . '"></td>';
}
echo "</table>";
You didn't show us where $myNumber comes from so if the variable name is different just modify it in the code below.
echo '<td>/images/image' . ($myNumber == 0 ? '3' : ($myNumber >= 1 ? '1' : '2')) . '.jpg</td>';
Try:
if($myNumber < 0) {
//show /images/image2.jpg
}
if($myNumber == 0) {
//show /images/image3
}
if($myNumber > 0) {
//show /images/image1.jmp
}
In your code:
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
echo "<td>";
if($row['percentchange'] < 0) {
//show /images/image2.jpg
}
if($row['percentchange'] == 0) {
//show /images/image3
}
if($row['percentchange'] > 0) {
//show /images/image1.jmp
}
echo "</td>";
}
This is assuming, based off of your other comments, that $row['percentchange'] is the number in particular that you care about checking against.
I'm struggling to create a table made of the joomla database. Currently I'm using the following code:
$query= "SELECT DATUM, A , B ,C, D, E, F, G FROM ".$db->quoteName('#__XYZTABLE'). " WHERE DATUM_ENG>= CURDATE()";
$db->setQuery($query);
$results = $db -> loadObjectList();
echo "<table><tr><th>Name</th><th>Department</th></tr>";
foreach($results as $row){
echo "<tr>"; echo "<td>".$row->last_name."</td>";
echo "<td>".$row->dept."</td>";
echo "</tr>"; }
echo "</table>";
Basically, this code works fine. The result is:
DATUM , A , B ,C, D, E, F, G
xxxx1 ,aa1,bb1,cc1,dd1,ee1,ff1,gg1
xxxx2 ,aa2,bb2,cc2,dd2,ee2,ff2,gg2
....
However, I want to create an html table which transposes the data and shows it like the following form:
xxxx1 , xxxx2, xxxx3 ....
A aa1 , aa2 , aa3
B bb1 , bb2 ,bb3
C cc1 , cc2 ,cc3
D .....
After searching in this forum and Google, I wasn't able to find anything similar. Can someone help me with a specific code?
Using some php array functions, you may take this:
$object = new stdClass();
// create some test values
$object->DATUM = '2016120';
$object->A = 'aa';
$object->B = 'bb';
$object->C = 'cc';
$object->D = 'dd';
$object->E = 'ee';
$object->F = 'ff';
$object->G = 'gg';
for ($i = 1; $i < 10; $i++){
$vars = get_object_vars($object);
$obj = new stdClass;
foreach ($vars as $key=>$var){
$obj->$key = $var.$i;
}
$results[] = $obj;
}
// end of creating test values
echo "<table><tr><th>DATUM</th><th>A</th><th>B</th><th>C</th><th>D</th></tr>";
foreach ( $results as $row ) {
echo "<tr>";
echo "<td>" . $row->DATUM . "</td>";
echo "<td>" . $row->A . "</td>";
echo "<td>" . $row->B . "</td>";
echo "<td>" . $row->C . "</td>";
echo "<td>" . $row->D . "</td>";
echo "<td>" . $row->E . "</td>";
echo "<td>" . $row->F . "</td>";
echo "<td>" . $row->G . "</td>";
echo "</tr>";
}
echo "</table>";
echo '<h3>New Variant</h3>';
echo '<table>';
$elements = count($results);
foreach (get_object_vars($results[0]) as $key=>$var){
echo '<tr>';
$tag = ($key === 'DATUM') ? 'th>' : 'td>';
echo '<'.$tag . $key .'</'.$tag;
for ($i = 0; $i < $elements; $i++){
echo '<'.$tag . $results[$i]->$key.'</'.$tag;
}
echo '</tr>';
}
echo '</table>';`
I managed to get the checkboxes to POST the data and displays each on the checkout page but the problem I have is when it gets to 4th column it stops when it hits a certain char limit also how would I turn this into a table on the checkout page.
Database snippet code:
print '<td><input type="checkbox" name="check_list[]"value='. $getColumn[0]. $getColumn[1]. $getColumn[2]. $getColumn[3]. $getColumn[4]. $getColumn[5].$getColumn[6].$getColumn[7].$getColumn[8].$getColumn[9].'</td>';
for ($column = 1; $column < pg_num_fields($res); $column++)
{
print "<td>" . $getColumn[$column] . "</td>";
}
}
print '</table>'
Checkout page
<?php
echo "<hr />\n";
$res = pg_query ($con, "select count(ref) from music");
$a = pg_fetch_row($res);
echo "<p>Total " . $a[0] . " music in database.</p>";
echo "<table border='1'>\n<thead>\n<tr>\n";
echo "<th>Artist</th><th>Composer</th><th>Genre</th><th>Title</th><th>Album</th><th>Label</th> <th>Price</th><th>Description</th>\n";
echo "</tr>\n</thead>\n<tbody>\n";
$res=pg_query($con, "SELECT * from music ORDER BY ref");
while ($a = pg_fetch_array ($res))
{
echo "<tr>";
for ($j = 0; $j < pg_num_fields($res); $j++) {
// htmlspecialchars converts things like & to HTML entity codes
echo "<td>" . htmlspecialchars($a[$j], ENT_QUOTES) . "</td>";
}
echo "</tr>\n";
}
echo "</tbody>\n</table>";
?>
I am sure, you are tried to do the following:
print '<table>';
for ($column = 1; $column < pg_num_fields($res); $column++) {
echo '<tr>';
print '<td><input type="checkbox" name="check_list[]" value="'.$getColumn[$column] .'" /></td>';
print "<td>" . $getColumn[$column] . "</td>";
echo '</tr>';
}
print '</table>';
I'm trying to parse the arrivals table from here [1] and put in into an array to be able to format it and put it into a table.
I did some research here and there, I've got some code from other questions, but I can't make the array and table look as I'd like.
Anyone can help me out?
<?php
require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');
$table = $html->find('table', 3);
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$rowData = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$rowData[] = $cell->innertext;
}
echo "<table>";
echo "<td>";
echo $rowData[0]. " ";
echo "</td>";
echo "<td>";
echo $rowData[1]. " ";
echo "</td>";
echo "<td>";
echo $rowData[2]. " ";
echo "</td>";
echo "<td>";
echo $rowData[3]. " ";
echo "</td>";
echo "<td>";
echo $rowData[4]. " ";
echo "</td>";
echo "<td>";
echo $rowData[5]. " ";
echo "</td>";
echo "<td>";
echo $rowData[6]. " ";
echo "</td>";
echo "<td>";
echo $rowData[7]. " ";
echo "</td>";
echo "<td>";
echo $rowData[8]. " ";
echo "</td>";
echo "</table>";
}
?>
Maybe try putting each row into an array and then each cell into another array. Hopefully, that will do what you want.
require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');
$table = $html->find('table', 3);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$flight = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$flight[] = $cell->plaintext;
}
$rowData[] = $flight;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
Note: this solution requires the simple_html_dom.php library. Get it here!