I am trying to visualise variables in PHP using isset(), when I am not using it, a notice comes out saying that the index is undefined.
I am trying to output variables in the tabled using the aforementioned function, but with it, the tables do not output any values.
[
<?php
// Include config file
require_once "config.php";
// Attempt select query execution
$sql = "SELECT * FROM courses";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>#</th>";
echo "<th>Enquiry ID</th>";
echo "<th>Course Name</th>";
echo "<th>Course Level</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . isset($row\['courseid'\]) . "</td>";
echo "<td>" . isset($row\['enquiryid'\]) . "</td>";
echo "<td>" . isset($row\['coursename'\]) . "</td>";
echo "<td>" . isset($row\['courselevel'\]) . "</td>";
echo "<td>";
echo "<a href='read.php?id=". isset($row\['courseid'\]) ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "<a href='update.php?id=". isset($row\['courseid'\]) ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='delete.php?id=". isset($row\['courseid'\]) ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
unset($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
unset($pdo);
?>
]1
I expect the output of the values included in the database tables, but the fields still continue blank.
isset() returns true or false. You can't echo it out directly. You need a conditional that uses isset() to check a variable and display it when true.
On the assumption you're running PHP 7, you can use the null coalescing operator. This acts as a shorthand for a ternary that either results in the variable or a blank string depending on whether the variable is set:
echo "<td>" . $row['courseid'] ?? '' . "</td>";
Documentation: https://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
This is the equivalent of:
echo "<td>" . isset($row['courseid']) ? $row['courseid'] : '' . "</td>";
If you were forced to use that ternary method however, I'd probably wrap it in a function for convenience.
This isset() Function Returns True/False, So you can not echo out the value directly.
So it's better to use single line condition with isset()
Like i've written below
echo "<td>" . isset($row['courseid']) ? $row['courseid'] : 'Not Set' . "</td>";
I rewrote your while loop as an example:
while($row = $result->fetch()){
echo "<tr>";
if (isset($row['courseid'])){
echo "<td>" . $row['courseid']) . "</td>";
} else {
echo "<td>" . 'NOT SET' . "</td>";
}
if (isset($row['enquiryid'])){
echo "<td>" . $row['enquiryid'] . "</td>";
} else {
echo "<td>" . 'NOT SET' . "</td>";
}
if (isset($row['coursename'])){
echo "<td>" . $row['coursename']. "</td>";
} else {
echo "<td>" . 'NOT SET' . "</td>";
}
if (isset($row['courselevel'])){
echo "<td>" . $row['courselevel']) . "</td>";
} else {
echo "<td>" . 'NOT SET' . "</td>";
}
echo "<td>";
echo "<a href='read.php?id=". $row['courseid'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "<a href='update.php?id=". $row['courseid'] ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='delete.php?id=". $row\'courseid'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
Hope that helps
Related
Hi i want to href after user click a button
This is my current code but it's not working. After I click, the confirmation will come out but it doesn't go to PHadmin_approveHospital.php after I click on.
echo "<input type=\"submit\" value=\"Approve\">";
Full Function
public function displayAllHospital() {
$sql = "SELECT * FROM hospital";
$result = #mysqli_query($this->conn, $sql);
echo "<table class='table table-bordered'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID # <i class='fa fa-sort'></i></th>";
echo "<th>Name </th>";
echo "<th>Email </th>";
echo "<th>Contact Number <i class='fa fa-sort'></i></th>";
echo "<th>Status </th>";
echo "<th>Actions</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>" . $row["HospitalID"] . "</td>";
echo "<td>" . $row["Hospitalname"] . "</td>" ;
echo "<td>" . $row["email"] . "</td>" ;
echo "<td>" . $row["contactno"] . "</td>" ;
echo "<td>" . $row["status"] . "</td>" ;
echo "<td>";
echo "<a href=\"PHadmin_editHospital.php?id=".$row["HospitalID"]."\" class='view' title='View' data-toggle='tooltip'><i class='material-icons'></i></a>";
echo "<a href=\"PHadmin_editHospital.php?id=".$row["HospitalID"]."\" class='edit' title='Edit' data-toggle='tooltip'><i class='material-icons'></i></a>";
echo "<a href=\"PHadmin_deleteHospital.php?id=".$row["HospitalID"]."\" onclick=\"return confirm('do you want to delete Y/N')\" class='delete' title='Delete' data-toggle='tooltip'><i class='material-icons'></i></a>";
echo "</td>";
echo "<td>";
if($row["status"] == "pending"){
echo "<input type=\"submit\" value=\"Approve\">";
}
echo "</td>";
echo "</tr>";
echo "</tbody>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
}
Thanks for helping.
You may call a javascript function when you click the button, and then display the confirmation message, so that the user can confirm or not (if confirmed, jump to the url)
You may use the following code :
<input type="button" value='Approve' onclick="javascript:check1();">
<script>
function check1() {
if(confirm("Do you want to approve?")) {
window.location.href="PHadmin_approveHospital.php?id=<?php echo $row["HospitalID"]. '\'; ?>";
}
}
</script>
So , for your updated code, it will be:
<?php
public function displayAllHospital() {
echo '
<script>
function check1(var1) {
if(confirm("Sure to delete ?")) {
window.location.href="PHadmin_deleteHospital.php?id=" +var1;
}
}
function check2(var2) {
if(confirm("Sure to approve ?")) {
window.location.href="PHadmin_approveHospital.php?id=" + var2;
}
}
</script>';
$sql = "SELECT * FROM hospital";
$result = #mysqli_query($this->conn, $sql);
echo "<table class='table table-bordered'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID # <i class='fa fa-sort'></i></th>";
echo "<th>Name </th>";
echo "<th>Email </th>";
echo "<th>Contact Number <i class='fa fa-sort'></i></th>";
echo "<th>Status </th>";
echo "<th>Actions</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>" . $row["HospitalID"] . "</td>";
echo "<td>" . $row["Hospitalname"] . "</td>" ;
echo "<td>" . $row["email"] . "</td>" ;
echo "<td>" . $row["contactno"] . "</td>" ;
echo "<td>" . $row["status"] . "</td>" ;
echo "<td>";
echo "<a href=\"PHadmin_editHospital.php?id=".$row["HospitalID"]."\" class='view' title='View' data-toggle='tooltip'><i class='material-icons'></i></a>";
echo "<a href=\"PHadmin_editHospital.php?id=".$row["HospitalID"]."\" class='edit' title='Edit' data-toggle='tooltip'><i class='material-icons'></i></a>";
echo "<input type=button value=Delete onclick='javascript:check1(". $row["HospitalID"] . ")';>";
echo "</td>";
echo "<td>";
if($row["status"] == "pending"){
echo "<input type=button value=Approve onclick='javascript:check2(". $row["HospitalID"] . ")';>";
}
echo "</td>";
echo "</tr>";
echo "</tbody>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
}
?>
I need to retrieve the color hex code stored in a database and display it as seen on screen within a table cell. i'm trying to find a way to convert the hex code the db query returns for that specific table cell into the actual color that the hex denotes. Here's my code...
<?php
// Include config file
require_once "config.php";
$pdo = new PDO($dsn, $username, $password, $options);
// Attempt select query execution
$sql = "SELECT * FROM sales";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>S/N</th>";
echo "<th>Transaction Date</th>";
echo "<th>Customer Name</th>";
echo "<th>Address</th>";
echo "<th>Phone Number</th>";
echo "<th>Vehicle Model</th>";
echo "<th>Vehicle Chassis Number</th>";
echo "<th>Vehicle Registration Number</th>";
echo "<th>Vehicle Color</th>";
echo "<th>Amount Paid</th>";
echo "<th>Advance</th>";
echo "<th>Balance</th>";
echo "<th>Balance Due Date</th>";
echo "<th>Paid To</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['transactiondate'] . "</td>";
echo "<td>" . $row['customername'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['vehiclemodel'] . "</td>";
echo "<td>" . $row['vehiclechassisnumber'] . "</td>";
echo "<td>" . $row['vehicleregistrationnumber'] . "</td>";
echo "<td>" . $row['vehiclecolor'] . "</td>";
echo "<td>" . $row['amountpaid'] . "</td>";
echo "<td>" . $row['advance'] . "</td>";
echo "<td>" . $row['balance'] . "</td>";
echo "<td>" . $row['balancedate'] . "</td>";
echo "<td>" . $row['paidto'] . "</td>";
echo "<td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
unset($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
unset($pdo);
?>
It is a bit unclear about where you wish to use the colour but I'm assuming it is here. And also my assumption is the retrieved value for vehiclecolor column is a HEX value
echo "<td>" . $row['vehiclecolor'] . "</td>";
You can use tag property 'background-color' for this.
$temp = $row['vehiclecolor'];
echo "<td style='background-color:" . $temp ."'></td>";
Pretty self-explanatory I guess. String concatenation.
I want to add "book" button to each car product.But it only display only one button for first car only.
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<td><button onclick=\"book_car('" . $row['car_id'] .
"')\">Book</button></td>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "</tr>";
}
There is no error.But i just want "book" button display for each car products.
This is simply because your button is out of the while loop !
Also you did not close first tr tag .
Correct code :
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>action</th>";<!-- Added this line -->
echo "</tr>";<!-- Added this line -->
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "<td><button onclick=\"book_car('" . $row['car_id'] .
"')\">Book</button></td>";<!-- Replaced This line -->
echo "</tr>";
}
echo "</table>";
I hope this helps you :)
Two questions in one:
First question, mainly about presentation:
I'm echo-ing the following code which should create a table. The table should have a single column, but it's being rendered with the elements above the image as a single line. can anyone see why?
<?php
$sql = "SELECT * FROM catdata WHERE featured='yes' LIMIT 2";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['manufacturer'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td><img src=\"6.diesel.png\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['size'] . "l</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['mileage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['fitsmodel'] . "</td>";
echo "</tr>";
echo "<tr class=\"tablePriceBlock\">";
echo "<td>£" . $row['pricefitted'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>£" . $row['pricedel'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else {
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Unable to execute $sql. " . mysqli_error($link);
}
?>
Question 2:
Can I show the second result in a second column, or as a separate table? Also, is it possible to access the $result elements like say, $[manufacturer][1]?
Always look at the emitted source your code generates by either "View Source" or using a tool like curl. You'll find this mistake:
echo "</tr";
You're missing a >.
Many people who write HTML have browser plugins that can link through to an HTML validator to ensure they've got the correct syntax. You may want to find and install one of these.
To generate the second result is a separate table follow the following code.
echo "<table>";
$count = 1;
while($row = mysqli_fetch_array($result)){
if($count == 2){
echo "<table>";
echo "<tr>";
echo "<td>put your code here</td>";
echo "</tr>"
echo </table>
}else{
echo "<tr>";
echo "<td>" . $row['manufacturer'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td><img src=\"6.diesel.png\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['size'] . "l</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['mileage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['fitsmodel'] . "</td>";
echo "</tr>";
echo "<tr class=\"tablePriceBlock\">";
echo "<td>£" . $row['pricefitted'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>£" . $row['pricedel'] . "</td>";
echo "</tr>";
}
$count ++;
}
echo "</table>";
As you are limiting your query to only two record so this method is not bad for that and hope this will help.
I have a PHP code, showing me raw data from a MySQL database in a table.
I want to add some text to one of the existing cells, depending on a value in another column which is not displayed in the table.
My code looks like this:
while($row = mysqli_fetch_array($rs))
{
echo '<tr class="lokbes">';
echo "<td class='blaa'>" . $row['Navn'] . "</td>"; // I want the extra text here.
echo "<td>" . $row['Stilling'] . "</td>";
echo "<td>" . $row['Institution'] . "</td>";
echo "<td><a href='mailto:$row[Email]'>" . $row['Email'] . "</a></td>";
echo "<td>" . $row['Mobiltelefon'] . "</td>";
}
echo "</tr>";
echo "</table>";
This outputs a table consisting of Name, job, workplace etc.
In the cell displaying the name, I would like to add some text if a column in my MySQL DB has the value 1 in the row.
What to do? I've tried using if, as seen below - but that doesn't seem to work.
echo "<td class='blaa'>" . $row['Navn'] .
if ($row['Formand'] == 1) {
echo "(Formand)";
} "</td>";
You have to do multiple echos :
echo "<td class='blaa'>" . $row['Navn'];
if ($row['Formand'] == 1) {
echo "(Formand)";
}
echo "</td>";
Or, with ternary operator :
echo "<td class='blaa'>" . $row['Navn'] . ($row['Formand'] == 1 ? "(Formand)" : "") . "</td>";
update like this.
echo "<td class='blaa'>" . $row['Navn'];
if ($row['Formand'] == 1) {
echo " (Formand)";
}
echo "</td>";
or you can use short PHP tag in HTML code.
?>
<td class="blaa">
<?php echo $row['Navn']?><?php echo ($row['Formand'] == 1)?' (Formand)':'';?>
</td>
<?php
Try below code.
while($row = mysqli_fetch_array($rs))
{
echo '<tr class="lokbes">';
echo "<td class='blaa'>" . $row['Navn']." ".($row['Formand'] == 1 ? "(Formand)" : ""). "</td>"; // I want the extra text here.
echo "<td>" . $row['Stilling'] . "</td>";
echo "<td>" . $row['Institution'] . "</td>";
echo "<td><a href='mailto:$row[Email]'>" . $row['Email'] . "</a></td>";
echo "<td>" . $row['Mobiltelefon'] . "</td>";
}
echo "</tr>";
echo "</table>";