so I'm trying to get my PHP array to print an entire array within one td, so I want the table to look like this:
First --- Last --- Values
John -- Smith -- 1,2,4,6
$comp_labs holds the values (1,2,4,6 for example)
Here's the code I've got at the moment:
echo "<tr>";
echo "
<td> ".$firstName."</td>
<td> ".$lastName."</td>
<td> ".$comp_labs."</td>
</tr>\n";
echo "</table>";
it's printing the name fine but only the last element of the array. I tried looping through just that td printing array[i] like so:
echo "<tr>";
echo "
<td> ".$firstName."</td>
<td> ".$lastName."</td>
<td> "for($i=0; $i < count($comp_labs); $i++){.$comp_labs[i].}"</td>
</tr>\n";
echo "</table>";
but it didn't like that. However if I just Echo out the $comp_labs array outside of the table it prints the entire array fine, which I find a little confusing. I've had a look online but every forum I've come across has been people trying to loop through and print 1 value per td, not all values in one td.
If anyone is able to help out here I would greatly appreciate it, thanks in advance!
You need implode()
<td> ".implode(",", $comp_labs )."</td>
So it becomes :
echo "<tr>
<td> ".$firstName."</td>
<td> ".$lastName."</td>
<td> ".implode(",", $comp_labs)."</td>
</tr>\n";
Implode the array turning it into a string.
$comp = implode(", ",$comp_labs);
Related
Sorry if using wrong terms below, hopefully you understand what I mean. Also, please note this is just a learning task and not actually used.
I have some JSON data which has been imported and decoded into an array. The array is about 20 elements of different products for a webshop which has multiple values (id,name,price,stock,category,discount etc). I want to print a list of all the items based on name. However, one of the elements does not have a name. The "name":"" is completely missing in the JSON data, therefore in my array too. So, it's not NULL, it just doesn't exist.
I have tried various of if functions (isset, !empty) and put usort and foreach inside the if loop, but I believe those I've tried demand that the certain value name actually exists but is not set and not that it is not there at all.
Code example to print list:
echo "<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>";
usort($product_data, fn($a,$b) => strcmp($a->name, $b->name) );
foreach($product_data as $product) {
echo "<tr>";
echo "<td>". $product->name ."</td>";
echo "<td>". $product->price ."</td>";
}
}
echo"</td>
</tr>
</tbody>
</table>";
I get this error no matter what I do:
Notice: Undefined property: stdClass::$name in [filename]
I'm thinking there must be a quick way to say "if 'name' exists do this (otherwise nothing)" but cannot find it at all. Please help!
What about $product->name ?? '';
It's like isset($product->name) ? $product->name : '';
solution may be
<pre>if(property_exists($product, 'name')){
//put here your td
echo "<td>". $product->name ."</td>";
}</pre>
use property_exists(object,attribtue betwen quotation) for checking if each attribute that you are fetching exist
So I have a html table that is automatically generated after passing a query to my database. I want to create a hyperlink within my html table to a page that will pull more detailed information from a Second Table.
I was thinking of using the Tablecell creator that pulls from the First Table, and modifying so that it would encompass the table's contents with hyperlink tags. I was thinking it would look like this.
foreach(new TableRow(new AutoArrayMaker($stmt->fetchAll()) as $rowend => $row){
echo <a href = "the reusable HTML Page">;
echo $row;
echo </a>;
}
Is my idea sound from a coding standpoint?
Firstly, echo's need to be in quotation marks " So that code wouldn't fire.
There are a few ways you can output HTML. The first is using echo's:
echo "Google";
Notice how I put a back-slash before hand? This is what is known as an escape. This puts the character after into a letter depending on what it escapes to. See php docs: http://php.net/manual/en/regexp.reference.escape.php (as my description of it was poor)
The other option would be to run out of php then join back on so to speak:
<?PHP
foreach(new TableRow(new AutoArrayMaker($stmt->fetchAll()) as $rowend => $row){
?>
<a href="abc">
<?PHP echo $row; ?>
</a>
<?PHP
}
However, this is not advised.
Edit:
Also, you can make your own table very simply:
<table>
<?PHP
foreach($stmt as $row){
?>
<tr>
<td>
<a href="abc"><?PHP echo $row[id]; ?>
</td>
</tr>
<?PHP
}
?>
</table>
See https://www.w3schools.com/html/html_tables.asp for more info.
I am a beginner and somehow made to get the query (php & Mysql) I want and using echo i got the output as few lines without difficulty. But now I want the output inside the cell of a table. I tried something like this:
This does not work:
<tr>
<th>subject</th>
<th>grade</th>
</tr>";
echo "<tr>";
echo "<td>".$Row['name1']."</td>;
echo "<td>".$Row['subject1'].</td>";
echo "</tr>";
echo "</table>";
Whereas this work:
echo $line['name1']."<tr></td>"."";
echo $line['subject1']."<tr></td>"."";
The echo $line statement echoes the value of name1 and subject1 without any difficulty. but the echo Row is not showing the output. As my data has only one row I dont have to use any loop. I actually want two fields in first row (name1 and subject1) and then in next row the fields of name2 and subject2 and till name7, subject7. It looks like the format inside the table is wrong. Could someone help me plz?
First of all replace
echo "<td>".$Row['name1']."</td>;
with
echo "<td>".$Row['name1']."</td>";
you are missing (") at the end before (;)
Updated with the missing table tag. Try this
<?php
echo '<table>';
echo '<tr>';
echo '<th>subject</th>';
echo '<th>grade</th>';
echo '</tr>';
echo "<tr>";
echo "<td>".$Row['name1']."</td>";
echo "<td>".$Row['subject1']."</td>";
echo "</tr>";
echo "</table>";
?>
Just to expand the current answers, I'd suggest you use a single echo and concatenate the strings or even better, just use one single string and concatenate only the necessary variables:
<?php
echo '
<table>
<tr>
<th>subject</th>
<th>grade</th>
</tr>
<tr>
<td>'.$Row['name1'].'</td>
<td>'.$Row['subject1'].'</td>
</tr>
</table>';
?>
This of course works better if the amount of PHP code is greater than the amount of HTML code. But if you were to write more HTML than PHP, it'd make more sense to just open and close <?php?> tags and echoing the variable you want.
I used an answer instead of a comment for the sake of the example. Feel free to try this approach when you are dealing with several html elements and need to insert your values within them.
This is my first code in php, so my problem might be so obvious. sorry if it is so easy :)
What I'm trying to do is that I am selecting some rows from my data base using
$rrows = Select ( "*" , $tbl_SubForum , null, "p");
$rrows->setFetchMode(PDO::FETCH_CLASS, 'subForum');
I know this works fine.
Each row has the description of a sub forum, containing title and id. I am trying to show sub forum titles in table cells using this code:
<table cellpadding=50px cellspacing=20px BORDER=0>
<?php
$i=0;
while($rrow = $rrows->fetch()){
var_dump($rrow);
?>
<tr>
<td class='subforum' id='subforum1'>
<?php echo $rrow["title"]; ?><br>
Sub forum manager<br>
Posts: 200<br>
Active users: 50<br>
</td>
</tr>
<?php
$i++;
}
?>
the line echo $rrow["title"]; doesn't work and so the page is empty, except for the result of the first var_dump
First var_dump of the first $rrow shows:
as you can see, there actually is a title field in the array and there is only one var_dump so the while loop doesn't work anymore!
why is this happening?
Because $rrow is an object rather than an array, you have to use $rrow->title to access its data member.
I want to create a site, which displays a lot of records from a database. To make it more reader-friendly, I want to use a styling. One record is white background, the next blue, the next hhite again.
So I tried this:
<?php while ($info = mysql_fetch_array($data)){
PRINT "<tr>";
PRINT "<td>" .$info['articlenr']. "</td>";
PRINT "<td>" .$info['stock']. "</td>";
PRINT "</tr>";
PRINT "<tr>";
PRINT "<td bgcolor=#0066FF>" .$info['articlenr']. "</td>";
PRINT "<td bgcolor=#0066FF>" .$info['stock']. "</td>";
PRINT "</tr>";
}
?>
This works for the view, but the problem is, the blue record is the same as the white, not the next one, it just doubles the record and make it another color.
How can I do this right?
Use :nth-of-type(even) to get even/odd combination of color's.
Here is a demo example:
html:
<table>
<tr><td>item1</td>
</tr>
<tr><td>item2</td>
</tr>
<tr><td>item3</td>
</tr>
<tr><td>item4</td>
</tr>
</table>
css:
tr:nth-of-type(even) { background-color: #0066FF; }
Demo
If you want to do this in PHP, you could do it like this :
<?php
$iter = 0;
$color1 = 'red'; //can se hex code too, like #0066FF;
$color2 = 'blue';
while ($info = mysql_fetch_array($data))
{
echo '<tr style="background-color:'.( ($iter%2==0) ? $color1 : $color2).';">';
// rest of the printing stuff
$iter++;
}
?>
Statement
($iter%2==0) ? $color1 : $color2
does this : it asks the question whether iterator (or row number) is even. If yes, the it takes color1. If not (row is uneven) it takes the second color.
PHP Smarty is good for doing this kind of stuff (iterating over colors and styles), but it may be difficult for beginners.
Please go through this links:
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started
http://css-tricks.com/complete-guide-table-element/
Including CSS in .php file
Can I offer some advice on your code? Your approach mixes PHP and HTML in a way that makes it difficult for your development environment to parse your HTML, and you can achieve the same with less keystrokes! Consider this approach instead:
<?php while ($info = mysql_fetch_array($data)): ?>
<tr>
<td><?php echo $info['articlenr'] ?></td>
<td><?php echo $info['stock'] ?></td>
</tr>
<?php endwhile ?>
Changes I've made:
Removed the duplicate row
Rendered everything in HTML mode, and opened PHP tags just for PHP code
Switched the loop to the colon form, which is often thought to be clearer in templates. Do carry on using the brace approach, however, in large chunks of code (e.g. classes)
Written PHP keywords in lower case
Used echo rather than print
Combine this with Joke_Sense10's answer for a full solution.