I am currently developing a system where I would like individual cells which are being pulled from a database to change color when either a check box is clicked.
I am assuming perhaps referencing to the current table and then adding a field where when the check box is click, the value turns to 1 and therefore it will change the color?
I've got as far as thinking an IF statement is needed, but i'm not sure what else. If there is a simple way, which will fit in the with the following code, that's even better.
$query = mysql_query("Select * from list Where id='$id'"); // SQL Query
$count = mysql_num_rows($query);
if($count > 0)
{
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print '<td align="center">'. $row['entry'] . "</td>";
Print '<td align="center">'. $row['title'] . "</td>";
Print '<td align="center">'. $row['full_name'] . "</td>";
Print '<td align="center">'. $row['location'] . "</td>";
Print '<td align="center">'. $row['startdate']. "</td>";
Print '<td align="center">'. $row['ipad']. "</td>";
Print '<td align="center">'. $row['laptop']. "</td>";
Print '<td align="center">'. $row['login']. "</td>";
Print '<td align="center">'. $row['frog']. "</td>";
Print '<td align="center">'. $row['sims']. "</td>";
Print '<td align="center">'. $row['email']. "</td>";
Print '<td align="center">'. $row['status']. "</td>";
Print "</tr>";
}
}
else
{
$id_exists = false;
}
If you need any further information I'll be more than happy to help.
look at my edit.
if you want to change only the color on clicking a checkbox you need javascript.
for updating data you need php code.
you have different options to update data of your database but all of them needs php code.
If you like to update the data witout reloading your page you need ajax.
The easier method is to update the data with a html form.
therefore you have to submit it, and on the destination page (which is definied in this format: <form action="index.php" ... >)
you can retrieve you value for the updating field.
Then store it with php in the database, and after it, you can show the html table.
then you can write you if statement to check which color you use in your cell.
which field do you change in your database?
you can use javascript to change the color of your cell when you click on a checkbox.
to change the value in your database you need ajax (no reload of your page)
or pure php.
One solution is to create a form and on click of your checkbox you can submit the form via javascript.
in action="index.php" (per example) you can write php code to change the value in your database based on the checkbox values are submitted.
EDIT:
To change color of a cell based on a database entry you can try this per example:
<td class="<?php echo($value == 1 ? 'green' : 'red'); ?>"></td>
in css you can define you css classes.
.green{
background-color: #f0f;
}
.red{
background-color: #f00;
}
Related
I am new to PHP and I am having trouble displaying results from a MySQL query.
So I have a table called Teams, and it contains two fields per entry:
Name, and Continent.
I send a query to the MySQL database to grab all the records where the Continent field equals "Asia." Then I am trying to print only the names of these teams in an HTML table.
Right now I can get the desired names, however my table has an extra empty column on the right and I don't understand how to get rid of it.
Here is my code:
$query =
"SELECT name from Teams WHERE continent='Asia'";
$result = mysql_query($query);
if(!$result){
echo 'Retreive Query Fail' . mysql_error();
}
?>
<div style="text-align: center; color: #000000">
Teams in Asia
</div>
<table>
<caption>
<dfn title="my table"></dfn>
</caption>
<tr>
<th>Team Names</th>
</tr>
<?php
while($row=mysql_fetch_array($result)){
echo '<tr>';
echo '<td>' . $row["name"] . '<td>';
echo '</tr>';
}
?>
</table>
And here is a picture of the result, the thing I want to get rid of is those boxes on the right of the table. (Please ignore the horrendous color scheme / layout. I have never done web design before.)
Close your <td> tag properly instead of adding another one:
while($row=mysql_fetch_array($result))
{
echo '<tr>';
echo '<td>' . $row["name"] . '</td>';
// ^ This guy.
echo '</tr>';
}
echo '<td>' . $row["name"] . '<td>';
should be
echo '<td>' . $row["name"] . '</td>';
I have a php script that connects to my database and returns results: name, and price, however, now I am trying to also create a link - that opens a new php page "more_info.php" and displays the correct description that corresponds to correct name.
I have been trying to accomplish this, but I haven't been able to get it working.
while($row = mysqli_fetch_array($result)) //mysql_fetch_array bring back an object, in this case the $result of the mysql query and puts it into a variable $row
{
echo "<td align='center'>" . $row['name'] . "</td> <td align='center'> <input type='button' value='More Info'; onclick=\"window.location='?start=' . $row['name.description'] . ?more_info.php?';\"> </td>";
within the onclick, I want the new window to open up, and bring in the description that matches the name, into the more_info.php
I input that code, but obviously it isn't working. Does this look like i'm on the right path at all?
Change it like this.
echo '<table>';
while($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td align="center">' . $row['name'] . '</td>';
echo '<td align="center">';
echo '<input type="button" value="More Info" onclick="window.location=\'more_info.php?start=' . urlencode($row['description']) . ' \';" />';
echo '</td>';
echo '</tr>';
}
echo '</table>';
and read it in more_info.php like:
<?php
$start = (!empty($_GET['start']) ? $_GET['start'] : false);
echo '<h1>Info about: ' . $start . '</h1>';
?>
shouldn't it be:
window.location='more_info.php?start=' . $row['name.description']
an alternative to using JS triggers on buttons for navigation is you could also just use a link and css style it to look like a button <a href="more_info.php?start=' . $row['name.description'].'" class="button">
My working PHP script connects to my database fetches the data, and then puts it into a table like so:
while($row = mysqli_fetch_array($result)) //mysql_fetch_array bring back an object, in this case the $result of the mysql query and puts it into a variable $row
{
echo "<tr>";
echo "<td align='center'><img height='100' width='100' src=\"" . $row['picturepath'] . "\" /></td>"; //here I can use the results and call out which column I want from my database
echo '<td align="center">' . $row['name'] . '</td>';
echo '<td align="center"><input type="button" value="More Info" onclick="window.location=\'more_info.php?start=' . urlencode($row['description']) .' \';" /></td>';
echo "<td align='center'>" . $row['price'] . "</td> <td align='center'> <input type='button' value='Add to Order' onclick=''> </td>";
echo "</tr>";
}
echo "</table>";
echo "<table width=\"1024\" align=\"center\" >";
echo "<tr height=\"50\"></tr>";
The "More Info" button links to a separate php script, more_info.php, where the description is read onto the page for that specific item. I wanted to also display the image that corresponds to that item, and have been having trouble doing so.
What I have tried.
I have modified the above code to display:
more_info.php?start=' . urlencode($row['description']) . urlencode($row['picturepath']) . '\';" /></td>';
which then pulls the correct information in, however it literally pulls in the filename of the image, rather than the actual picture.
Goal
I'd like to have the "More Info" link, pull the description and image of the item the user clicks 'more info' on, and dump both into "more_info.php". Would I have to use $_POST in this scenario? Or maybe on the more_info.php run a query where the picturepath is displayed only if it matches the name of the item? I'm not sure...
tough to tell whats going on in more_info.php without seeing the code, but you need a way to differentiate the 2 fields (description, picturepath) in that php script. for the url use something like:
$url = "more_info.php?start=" . urlencode($row['description']) . "&path=" . urlencode($row['picturepath']);
use $url string in your onclick:
... onclick="window.location=\'$url\'" ...
then in the more_info.php use $_GET to retrieve the 2 parameters (start, path) passed as part of the URL:
if(isset($_GET['start']) && isset($_GET['path'])){
$description = $_GET['start'];
$picturepath = $_GET['path'];
echo "Description: $description <img src=\"$picturepath\"/>";
}
Not sure if this helps/was what you were looking for. if not provide the more_info.php and it's current output.
I have a table in generated using PHP using data collected from MYSQL. How do I append a hyperlink to a row?
Here is the code for the dynamic table I am using:
mydata = mysql_query($sql,$con);
echo "<table id='name',table border=0>
<tr>
<th>Users</th>
<th>Status<th>
</tr>";
while($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['user_id'] . "</td>";
if (strtolower(trim($record['activity']))!=strtolower('LOGIN')){
echo "<td>" . $default1 . "</td>";
}else{
echo "<td>" . $default . "</td>";
}
echo "</tr>";
}
echo "</table>";
;
I have tried appending a href="..." style="display:block;"> but cannot get it to work.
Try echoing out a hyperlink.
echo "<a href='http://www.google.com/'>Google</a>";
If you want other functionality, such as clicking on a table row to work as a hyperlink, you would need to implement that in javascript as a table cell/row is not a hyperlink.
This is my table.
<?php
while(($result = mysqli_fetch_assoc($query))){
echo '<tr>';
echo '<td>';
echo $result['serial'];
echo '</td>';
echo '<td>';
echo $result['address'];
echo '</td>';
echo '<td>';
echo ''.$result['name'].'' ;
echo '</td>';
echo '<td>';
echo $result['postal'];
echo '</td>';
echo '<td>';
echo $result['website'];
echo '</td>';
echo '</tr>';
}
?>
Now I want that if a user moves his mouse over one row, the color should change.
If you don't need to support IE 6, add this to your CSS:
table tr:hover {
background:orange;
}
Make your <tr> tags <tr onMouseOver="this.bgColor='#EABF4E';">, or use table tr:hover in CSS.
I am a bit of a noob, but I think you need to give the table row a class, then give that class a hover property in your css file.
Add a class to your table row like so:
echo '<tr class="highlighter">';
You can name it anything, just make sure you use the same name in your css file.
Now, style the class so that it's color changes when a user's mouse hovers over it:
.highlighter:hover {
background: #ffff99;
}