Fetch First record in $key as $value assoc_arr - php

I have the Assoc_arr and the values in the array is:-
Array
(
[SIZE] => 8.5x11
[FOLDING] => HalfFold To 4.25x11
[PAPER] => 100lb Gloss Book with Aqueous Coating (C2S)
[COLOR] => full/full
[TURNAROUND] => Standard
)
When i print this it prints all the values with this code:-
echo "<table>";
foreach ($final_array as $key => $value)
{
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table><br><br>";
How can i only fetch the [SIZE] => 8.5x11 record from the assoc_arr.

Try this code
echo "<table>";
echo "<tr>";
echo "<td>";
echo key($arr);
echo "</td>";
echo "<td>";
echo $arr[key($arr)];
echo "</td>";
echo "</tr>";
echo "</table><br><br>";

You can try the following code
echo "<table>";
foreach ($array as $key => $value)
{
if('SIZE' == $key) {
echo "<tr><td>";
echo $key;
echo "</td><td>";
echo $value;
echo "</td></tr>";
}
}
echo "</table>";

You could use array_keys to get the first key and value:
$keys = array_keys($final_array);
$key = $keys[0];
$value = $final_array[$key];

Check with this:
echo "<table>";
foreach ($final_array as $key => $value)
{
if('SIZE' == $key) {
echo "<tr><td>";
echo $key;
echo "</td><td>";
echo $value;
echo "</td></tr>";
} else {
// do something else
}
}
echo "</table><br><br>";
UPDATE
If you want only the value for SIZE key, then no need of any loop. You can get it $final_array['SIZE'], otherwise the above thing will help you.

Related

PHP--How do I put form information into an HTML table once it has been saved to a text file?

This is just a block of code I am struggling with. Not sure if I am correct while trying to get this to format into an HTML table. I've messed with this for the last two hours trying to get it to work, I still don't have a clue what I am doing wrong. I am trying to get the information that comes from my array posted into the HTML table.
$ExistingSig = array('Name'=>'1','Version'=>'2','Hardware'=>'3', 'System'=>'4', 'Frequency'=>'5', 'Solution'=>'6');
$Report = array('Name'=>'a','Version'=>'b','Hardware'=>'c', 'System'=>'d', 'Frequency'=>'e', 'Solution'=>'f');
echo "<table border='1'>";
$count1=0;
foreach ($ExistingSig as $key => $value)
{
echo "<tr>";
if(!is_array($value))
{
if($key == 'Name' or $key == 'Version' or $key == 'Hardware' or $key == 'System' or $key == 'Frequency' or $key == 'Solution')
{
echo "<td>";
echo $key . ':' . $value;
echo "<br />\n";
echo "</td>";
}
}
$count2=0;
foreach ($ExistingSig as $key => $value)
{
if($count1==$count2){
//to eliminate array to string conversion error
if(!is_array($value))
{
if($key == '$AddName' or $key == '$AddVer' or $key == '$AddHard' or $key == '$AddSys' or $key == '$AddFreq' or $key == '$AddSol')
{
echo "<td>";
echo $key . ':' . $value;
echo "<br />\n";
echo "</td>";
}
}
}
$count2++;
}
echo "</tr>";
$count1++;
}
echo "</table>";
See if this works for you
<?php
$ExistingSig = array('Name'=>'1','Version'=>'2','Hardware'=>'3', 'System'=>'4', 'Frequency'=>'5', 'Solution'=>'6');
$Report = array('Name'=>'a','Version'=>'b','Hardware'=>'c', 'System'=>'d', 'Frequency'=>'e', 'Solution'=>'f');
$wantedKeys = array('Name','Version','Hardware','System','Frequency', 'Solution');
$thead = "<thead>";
$tbody="<tbody>";
foreach ($ExistingSig as $key => $value){
if(in_array($key,$wantedKeys)){
$thead .= "<th>$key</th>";
$tbody .= "<td>$value</td>";
}
}
echo "<table border='1'>$thead</thead>$tbody</tbody></table>";
?>

Print foreach value of array in one row

I have a following code
foreach($arr['transactions'] as $a){
foreach($a as $key => $value){
echo "<tr><td>" . $value . "</td></tr>";
}
}
Which gives output in one column.
Now what I want is print these values like below
416990962 COMPLETED Business Send 0183366139 0183366139 -1 0 0 655.99
Can anyone help me regarding this?
Try this
foreach($arr['transactions'] as $a){
echo '<tr>';
foreach($a as $key => $value){
echo "<td>" . $value . "</td>";
}
echo '</tr>';
}
You are getting result in one column because you are using <tr> inside the second loop. you need to use <tr> outside the second loop as:
<?php
foreach($arr['transactions'] as $a)
{
?>
<tr>
<?php
foreach($a as $key => $value)
{
?>
<td><?php echo $value;?></td>
<?php
}
?>
</tr>
<?php
}
?>
Like this:-
echo '<tr>';
foreach($a as $key => $value){
echo "<td>" . $value . "</td>";
}
echo '</tr>';

why does foreach skip 2 rows from the query result?

For 10 rows in the query it only returns 8 rows but i get the fields right:
For Query data which returns less than 2 rows I get an error.
//create table to display all data
echo "<table border="1"> ";
echo "<tr>";
$row = mysqli_fetch_assoc($result);
foreach($row as $key => $value)
{
echo "<th>$key</th>";
}
echo "</tr>";
while (($row = $result->fetch_assoc()) !== null)
{
$output = array();
$i=1;
echo "<tr>";
foreach ($row as $columnName => $columnValue)
{
$output[] = $columnName."=>". $columnValue;
echo "<td>".$columnValue."</td>";
}
echo "</tr>";
}
echo "</table>";
Edit: Thanks to #Michael Berkowski for his comments on the question.
Here is a modified version of your code that should work:
//create table to display all data
echo "<table border=1 >";
echo "<tr>";
//echo "<th> ## </th>";
$row = $result->fetch_assoc(); // stick to the object-oriented form. It is cleaner.
foreach ($row as $key => $value)
{
echo "<th>$key</th>";
}
echo "</tr>";
do
{
$output = array();
echo "<tr>";
foreach ($row as $columnName => $columnValue)
{
$output[$columnName] = $columnValue; // this is neater.
echo "<td>" . $columnValue . "</td>";
}
echo "</tr>";
} while ($row = $result->fetch_assoc());
echo "</table>";
You can use your first foreach() loop to print the keys and then use a do-while() loop to get your desired output.
Additional reading:
PHP do-while loop
You need to use
mysqli_fetch_assoc($result);

Simple html dom parser table to array

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!

Print query with loops in it and condition

I have a foreach function which permits me to print fields in a database,
I want to do possible that if $row[value]="photo" don't just print the $row[value] but another thing.
Can I do it?
The code is this:
while($row = mysql_fetch_array($result)) {
echo "<tr>";
foreach($checked1 as $key => $value){
echo "<td>" . $row[$value] . "</td>";
}
I need to have a condition that if $row[value]=photo to echo another thing not: echo "<td>" . $row[$photo] . "</td>";
Any example?
Since i could not get what to print for if($row['value'] == photo) check, so add that code yourself in the code written below.
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($value == 'photo'){
echo "<td><img src = '".$row[$value]."'></td>";
}else{
echo "<td>" . $row[$value] . "</td>";
}
}
}
This doesn't need to be complex.
foreach ($result as $row) {
if ($row['value'] === 'photo') {
// print "another thing"
} else {
// print "something"
}
}
Sure, just include an if statement with that. Your code will be similar to the code below:
$arr = array("one", "two", "three");
foreach ($arr as $value) {
if($value == "one")
echo "Not gonna print one.";
else
echo "Value: $value<br />\n";
}
Using your code, you can see if the value is not photo, echo it.
while($row = mysql_fetch_array($result)) {
echo "<tr>";
foreach($checked1 as $key => $value){
if($row[value] == "photo") {
echo "some other thing";
} else {
echo "<td>" . $row[$value] . "</td>";
}
}
}
If you want to print img src if the field is "photo" field (key) then
Try this :
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($key == 'photo'){
echo "<td><img src = '".$row[$value]."'></td>";
}else{
echo "<td>" . $row[$value] . "</td>";
}
}
}

Categories