I am new to mysqli I think I have the basic's under me but I am battling to a button in a table that i can execute.
This is my code:
while($row = mysqli_fetch_array($result))
{
echo "<tr align='left'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['sysdate'] . "</td>";
echo "<td>" . $row['systime'] . "</td>";
echo "<td>" . $row['controller'] . "</td>";
echo "<td>" . $row['sla_client'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" **BUTTON TO BE INSERTED HERE** "</td>";
echo "</tr>";
}
The below code obviously is not working in the above table
<button onclick="window.location.href = '../master.php';">Click Here</button>
You may find below solution:
while($row = mysqli_fetch_array($result))
{ ?>
<tr align='left'>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['sysdate']; ?></td>
<td><?php echo $row['systime']; ?></td>
<td><?php echo $row['controller']; ?></td>
<td><?php echo $row['sla_client']; ?></td>
<td><?php echo $row['notes']; ?></td>
<td><?php echo $row['id']; ?></td>
<td>BUTTON text TO BE INSERTED HERE</td>
</tr>
<?php }
Summary:
Just don't put Too much HTML in PHP, it's bad practice.
I would suggest: You could use something like this with HTML "href" tag.
Below is the Example of how to do it:
while($row = mysqli_fetch_array($result))
echo "<tr align='left'>";
echo "<td>".$row['id'] . "</td>";
echo "<td>".$row['sysdate'] . "</td>";
echo "<td>".$row['systime'] . "</td>";
echo "<td>".$row['controller'] . "</td>";
echo "<td>".$row['sla_client'] . "</td>";
echo "<td>".$row['notes'] . "</td>";
/* you could also use something like this with HTML href Tag */
echo "<td><a href=master.php><button>Click Here</button></a></td>";
echo "</tr>";
}
Related
Im would like to style a table row based on the value in the first cell.
order_status has 3 options. 'Done' 'Optie' 'Def'
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['order_status'] . "</td>";
echo "<td>" . $row['planning'] . "</td>";
echo "<td>" . $row['order_name'] . "</td>";
echo "<td>" . $row['order_number'] . "</td>";
echo "<td>" . $row['order_pl'] . "</td>";
echo "<td>" . $row['order_date_in'] . "</td>";
echo "<td>" . $row['order_date_out'] . "</td>";
echo "<td>" . $row['order_location'] . "</td>";
echo "<td>" . $row['order_customer'] . "</td>";
echo "<td>" . $row['order_contact'] . "</td>";
echo "<td>" . $row['order_content'] . "</td>";
echo "</tr>";
}
echo "</table>";
If you add a class to the row you can easily do this by updating your CSS file.
PHP
Update your PHP to look like this:
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr class=\"order_status_{$row['order_status']}\">
<td> {$row['order_status']} </td>
<td> {$row['planning']} </td>
<td> {$row['order_name']} </td>
<td> {$row['order_number']} </td>
<td> {$row['order_pl']} </td>
<td> {$row['order_date_in']} </td>
<td> {$row['order_date_out']} </td>
<td> {$row['order_location']} </td>
<td> {$row['order_customer']} </td>
<td> {$row['order_contact']} </td>
<td> {$row['order_content']} </td>
</tr> ";
}
echo "</table>";
The following line...
class=\"order_status_{$row['order_status']}\"#
Will give class names in the format of order_status_*; for example:
order_Status_confirmed
order_Status_received
CSS
Then you can add something similar to the following to your CSS and control exactly how it looks.
.order_status_pending{
background: #faa;
}
.order_status_confirmed{
background: #afa;
}
i am trying this from 5hrs don't know i am going wrong.My table echos results but not echo total for the same.
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr><?php
(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
(I get these entreis correct )
}
}
(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
?></table>
Remove the } } after the closing tag of the tr.
if you can't close the whole PHP block then you probably did something wrong in your syntax.
I don't know if you have more code or not, if you do - post it so we will get more accurate view of your code.
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr>
<?php
(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
(I get these entreis correct )
(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
}
?>
</table>
You can remove the wrong bracket, OR check that the two bracket are opened when fetching data
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr><?php
//(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
//(I get these entreis correct )
//(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
?>
</table>
if (mysqli_num_rows($res) > 0) {
while ($row = mysqli_fetch_assoc($res)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['second_name'] . "</td>";
echo "<td>" . $row['contact_no'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['date_created'] . "</td>";
echo "<td> <a href='delete.php' id=".$row['id']."'>Delete</a></td>";
echo "</tr>";
}
}
Send value to delete.php like below
echo "<td> <a href='delete.php?id=".$row['id']."' id=".$row['id']."'>Delete</a></td>";
And get value of id on delete.php with like below
$id = $_GET['id'];
try this
echo "<td> <a href='delete.php' id='".$row['id']."'>Delete</a></td>";
You simply assign that id value to one PHP variable and you can call that anywhere you want.
if (mysqli_num_rows($res) > 0) {
while ($row = mysqli_fetch_assoc($res)) {
$id=$row['id'];
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['second_name'] . "</td>";
echo "<td>" . $row['contact_no'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['date_created'] . "</td>";
echo "<td> <a href='delete.php' id='$id'>Delete</a></td>";
echo "</tr>";
}
}
You can use get variable at the end of url and then use it another page as $_GET['variable']
for more information http://php.net/manual/en/reserved.variables.get.php
so change your code like this
<?php
if (mysqli_num_rows($res) > 0)
{
while ($row = mysqli_fetch_assoc($res)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['second_name']; ?> </td>
<td><?php echo $row['contact_no']; ?> </td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['date_created']; ?></td>
<td>Delete</td>
</tr>
<?php }
} ?>
on next page get the id of that button as $id = $_GET['id'];
I am new to PHP and I have problem. I have a table with check boxes. I need to add if statement inside the check box.
echo "<div class='table1'>
<table>
<tr>
<td></td>
<td>Module code</td>
<td>Module Title</td>
<td>Option</td>
</tr>";
echo "<form action='confirmsubmission.php' method='post'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='check[]'
value='".$row['module_id']."' />" . "</td>";
echo "<td>" . $row['module_id'] . "</td>";
echo "<td>" . $row['module_title'] . "<a href=# content='".$row['description']."'
class='tooltip'><span title='Module Description'><img src='images/i.png'/></span>
</a>". "</td>";
echo "<td>" . $row['module_choice'] . "</td>";
echo "</tr>";
}
echo "</table></div>";
Below is a If statement I need to add after value='".$row['module_id']."'
if($row['module_choice']=='Mandatory'){ echo "checked=\"true\""; }
A bit of a mouthful, but try:
...
echo "<td>" . "<input type='checkbox' name='check[]' value='".$row['module_id']."'".($row['module_choice']=='Mandatory' ? 'checked="true"' : "")." />" . "</td>";
...
Even though I'll advise you to use a templating system (Smarty, etc.), you can do the following code:
echo "<div class='table1'>
<form action='confirmsubmission.php' method='post'>
<table>
<tr>
<td></td>
<td>Module code</td>
<td>Module Title</td>
<td>Option</td>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='check[]'
value='".$row['module_id']."'";
if($row['module_choice']=='Mandatory'){
echo " checked='checked' ";
}
echo "/>" . "</td>";
echo "<td>" . $row['module_id'] . "</td>";
echo "<td>" . $row['module_title'] . "<a href=# content='".$row['description']."'
class='tooltip'><span title='Module Description'><img src='images/i.png'/></span>
</a>". "</td>";
echo "<td>" . $row['module_choice'] . "</td>";
echo "</tr>";
}
echo "</table></form></div>";
I want to display data in database in table form. I wrote the following code
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['EmpID'] . "</td>";
echo "<td>" . $row['EmpName'] . "</td>";
echo "<td>" . $row['Designation'] . "</td>";
echo "<td>" . $row['Salary'] . "</td>";
echo "<td><a href='upasg3.php?empid='". $row['EmpID'] .">Edit</a ></td>";
echo "<td><a href='upasg3.php?empid='". $row['EmpID'] .">Delete</a ></td>";
echo "</tr>";
}
But i dont get the value of $row['EmpID'] concatenated with the value of href. It redirects to the upasg3.php with empid= blank.
how to fix this? Please help
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['EmpID'] . "</td>";
echo "<td>" . $row['EmpName'] . "</td>";
echo "<td>" . $row['Designation'] . "</td>";
echo "<td>" . $row['Salary'] . "</td>";
echo "<td><a href='upasg3.php?empid=". $row['EmpID'] ."'>Edit</a ></td>";
echo "<td><a href='upasg3.php?empid=". $row['EmpID'] ."'>Delete</a ></td>";
echo "</tr>";
}
quotes.
Try removing the single quotes after empid=
And putting them here like this:
echo "<td><a href='upasg3.php?empid=". $row['EmpID'] ."'>Edit</a ></td>";
echo "<td><a href='upasg3.php?empid=". $row['EmpID'] ."'>Delete</a ></td>";
This will give you an output like:
<a href='upasg3.php?empid=100'>
Try
echo "<td><a href='upasg3.php?empid=". $row['EmpID']."'>Edit</a ></td>";
^ ^
<table width="100%" border="1">
<tr>
<td>ID</td>
<td>NAME</td>
<td>Designation</td>
<td>Salary</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<tr>
<td><?=$row['EmpID']?></td>
<td><?=$row['EmpName']?></td>
<td><?=$row['Designation']?></td>
<td><?=$row['Salary']?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
try this
echo "<td><a href='upasg3.php?empid=". $row['EmpID'] ."'>Edit</a ></td>";
echo "<td><a href='upasg3.php?empid=". $row['EmpID'] ."'>Delete</a ></td>";
Your quotes were in wrong place.