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.
Related
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.
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);
I am trying to make the entire row of a table clickable. I have looked at a bunch of tutorials and they all seem pretty simple but I can't seem to get it to work. I tried DoNav and that didn't work, the simple onclick attribute doesn't work, what am I doing wrong? I am using mysql, php, and html. Thank you for your help!
echo '<table>';
echo '<tr>';
echo '<th>Name</th><th>Checked In?</th><th>ID</th>';
echo '</tr>';
while($row = $CheckedIn->fetch_assoc()){
echo '<tr onmouseover="ChangeColor(this, true);" onmouseout="ChangeColor(this,false);" onclick="document.location="www.engineering.us/gena/details.php";">';
echo '<td align="left">';
echo $row['ChildName'];
echo '</td>';
echo '<td align="center">';
;
echo $row['checkedin'];
echo '</td>';
echo '<td align="center">';
echo $row['P_Id'];
echo '</td>';
echo '</tr>';
}
echo '</table>';
onclick="document.location="www.engineering.us/gena/details.php";"
this should be
onclick="document.location='www.engineering.us/gena/details.php';"
Also another tip, you have more HTML than php, so it is better to use multiple php tags rather than echo called many times. That way it would be little easier to find such mistakes.
Edit
You should also escape the apostrophe in case you are still going with php
Picking up from what georoot said, use PHP within your HTML. It makes the markup a lot more readable and usually allows your program to add syntax highlighting, making it easier to read. I've added two additional classes ("js-table" and "table-tr") which I'll explain in a bit. Another important point is that the URL is in a new attribute on the table row: data-url.
<table class="js-table">
<thead>
<tr>
<th>Name</th>
<th>Checked In?</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<?php while ($row = $CheckedIn->fetch_assoc()): ?>
<tr class="table-tr" data-url="http://www.engineering.us/gena/details.php">
<td><?php echo $row['ChildName']; ?></td>
<td><?php echo $row['checkedin']; ?></td>
<td><?php echo $row['P_Id']; ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
The "table-tr" class allows us to target the table rows in CSS. There is no need to use JavaScript for something so simple. Even changing the colour of the text within the table cell is very easy in CSS. This technique has the other advantage that it can be easily changed without having to modify any HTML, PHP or JavaScript - it's a purely stylistic thing.
.table-tr:hover {
background-color: red;
}
Finally, since you're using jQuery, we can take advantage of that new "js-table" class. The "js-" prefix states that the class is used for binding functionality and it should not be styled. This script simply bind a click event to the table that listens out for a user clicking on a table row with the data-url attribute. When that happens, the page is re-directed (using window.location instead of document.location - it may work either way, but I've always used window.location) to that URL. This allows you to change the URL later on or change it per row without having to change any of your JavaScript.
$(function () {
$(".js-table").on("click", "tr[data-url]", function () {
window.location = $(this).data("url");
});
});
I hope that helps.
Consider using the following code:
$("#Table tr").click(function () {
$(this).addClass('selected').siblings().removeClass('selected');
var value = $(this).find('td:first').html();
window.location.href = "url";
});
Where Table is the your table name and url is the address you want to navigate to
I think you should try to do that with JQuery.
HTML
<tr class="table-tr"></tr>
JQuery
$('.table-tr').on('click', function(){
window.location = "your url here";
});
Please Change your code with this
<tr onclick="window.location.href=www.engineering.us/gena/details.php">
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.
I want to grab data from a mysql database by using php. The data looks something like this:
apple 3
orange 2
banana 4
I want to take the data and put it in a html table and use css to make it look pretty, but I dont want to deal with it inside <?php ?>
After I grab the
$result = mysql_query("SELECT * FROM Table");
can I reference the result variable outside the <? php ?> tags?
No. PHP can only be done in <?php ... ?> or <?= ... ?>. Use a template engine such as Smarty if you want substitution in this manner.
in short, no you cant, it is a php variable (technically a resource in this case) so you have to parse it through the php engine, which requires the php tags
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr><td>'.$row['fruit'].'</td><td>'.$row['id'].'</td></tr>';
}
echo '</table>';
Short answer is no. HTML cannot deal with dynamic content.
If you want to cut down the amount of echo statements within your code you can store the html within a given variable and then make reference to it.
I find it better to do the following:
<table>
<?php foreach($result as $row): ?>
<tr>
<td><?php echo $row['fruit']?></td>
<td><?php echo $row['id']?></td>
</tr>
<?php endforeach; ?>
</table>
This provides clarity and minimizes concatenation.