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>';
Related
I have some query that extracts data from the database like this:
|17DATE00 |concat(filename, fileextension) |
|-----------|--------------------------------|
|2017-05-30 |filename00000.pdf |
|2017-03-29 |filename00002.doc |
Now for the second column, I have passed the link of the doc, so you can click on it and download it.
This is the way I extract the data
$header = array("17DATE00", "concat(filename, fileextension)");
echo "<thead><tr>";
foreach ($header as $list) {
echo '<th >' . $list . '</th>';
}
echo "</thead></tr>";
foreach ($query as $key => $value) {
foreach ( $value as $a ) {
$url = get_template_directory_uri();
echo '<td>'. htmlspecialchars($a) .' </td>';
}
echo '</tr>';
}
Doing this, both columns get hyperlinked, while I only need the second one.
Anyway to fix this?
Add a counter to you inner loop:
$counter = 0;
foreach ( $value as $a )
{
$url = get_template_directory_uri();
if ($counter == 1)
{
echo '<td>'. htmlspecialchars($a) .' </td>';
}
else
{
echo '<td>'. htmlspecialchars($a) .'</td>';
}
$counter++;
}
You should print the link only if $key == "concat(filename, fileextension)"
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.
I want to display the column names along with the values in the PHP page.
while($get_info = mysql_fetch_row($orderdetails))
{
foreach ($get_info as $field)
{
echo "<td>" . $field . "</td>";
}
echo '</tr>';
}
This fetches just the values. How do I display the column names too?
The column names are order_id, productid, product_discount, amount, customerid, order_date.
while($get_info=mysql_fetch_array($orderdetails))
{
foreach ($get_info as $key => $val)
{
echo "<td>" .$key. ': ' . $val . "</td>";
}
echo '</tr>';
}
You are just missing the key from your foreach :
while($get_info=mysql_fetch_assoc($orderdetails))
{
foreach ($get_info as $field => $value)
{
echo "<td>" .$field.': '.$value."</td>";
}
echo '</tr>';
}
You might want to review your foreach php documentation for more information:
http://php.net/manual/en/control-structures.foreach.php
If you want each field to contain the column name, change it to mysql_fetch_array and do:
foreach($get_info as $key => $value) {
echo "<td>$key: $value</td>";
}
If you want the column names to be at the top of the table, you can either check the first row (if you're sure the table won't be empty):
$first = true;
while($get_info = mysql_fetch_assoc($orderdetails)) {
echo '<tr>';
if($first) {
$first = false;
foreach(array_keys($get_info) as $columnName) {
echo '<th>' . $columnName . '</th>';
}
echo '</tr><tr>';
}
foreach($get_info as $field) {
echo '<td>' . $field . '</td>';
}
echo '</tr>';
}
If you're not sure that the table will have at least one element, I would use a second DESCRIBE query.
while ($get_info=mysql_fetch_assoc($orderdetails))
{
foreach ($get_info as $columnName => $field)
{
echo "<td>$columnName: $field</td>";
}
echo '</tr>';
}
Notice that I am using mysql_fetch_assoc() which fetches the row having the column names as the keys.
<?php
while($get_info=mysql_fetch_array($orderdetails))
{
foreach ($get_info as $key => $val)
{
echo "column is " .$key. 'and value is ' . $val ;
}
echo '</br>';
}
?>
A lot of people are using a key => val demo but your code will work its just that your $field is now an a key so you need to tell it what column to look at.
When echoing just go
echo "<td>" . $field->column . "</td>";
That should work.
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>";
}
}
}
I need help figuring out these php print (echo) statements and where to place them. I have an embedded function 'strotime' that is transforming time (column 'StartTime') to a format, but I cannot get it to print out correctly. No errors, just no changes or use of the function.
Can someone help me figure out where to place this properly in this foreach loop?
(as you can see, i placed at beginning and tried an if statment too..but no luck). Thanks for
your help.
$keys = array('Server', 'Target','Logdate','Set','StartTime', 'Length','Size','Status');
echo '<table><tr>';
foreach ($keys as $column)
echo '<th>' . $column . '</th>';
echo '</tr>';
foreach ($data as $row){
echo '<tr>';
foreach ($keys as $column)
//if ($column == 'StartTime') {
// echo '<td>' . date("Y-m-d H:i:s",strtotime($row[$column])) . '</td>';
if (isset($row[$column])){
echo '<td>' . $row[$column] . '</td>';
} elseif ($column =='StartTime') {
echo '<td>' . date("Y-m-d H:i:s",strtotime($row[$column])) . '</td>';
} elseif ($column == 'Status') {
echo '<td> Check for Errors </td>';
} else {
echo '<td> </td>';
}
//}
}
echo '</table>';
In the beginning if foreach ($data as $row){ loop, do this:
$row['StartTime'] = date("Y-m-d H:i:s",strtotime($row['StartTime']));
And then display it like any other column.
Change
if (isset($row[$column])) {
to
if (isset($row[$column]) && $column != "StartTime") {
and by the way: you're missing the </tr> tags.