Set or Assign ID or REL inside PHP code - php

I want to set or assign id / rel inside my table code, take a look on my code below this so every table td has id and rel so I can give statement and manipulate the date within the jQuery, any helps will be much more appreciated:
function printdata()
{
if(is_array($_SESSION['chart']))
{
echo "Here's your chart" . "<br>";
$max = count($_SESSION['chart']);
$th1 = "<th>" . "No" . "</th>";
$th2 = "<th>" . "Nama Barang" . "</th>";
$th3 = "<th>" . "Harga Barang" . "</th>";
$th4 = "<th>" . "Stok Barang" . "</th>";
echo "<table border=1>";
echo "<tr>";
echo $th1 ;
echo $th2;
echo $th3;
echo $th4;
echo "</tr>";
for ($indexo = 0; $indexo < $max; $indexo++) {
echo "<tr>";
echo "<td>" . $indexo."</td>";
echo "<td >" . $_SESSION['chart'][$indexo]['namabarang']."</td>";
//i want to assign td rel inside of this echo
echo "<td>" . $_SESSION['chart'][$indexo]['hargabarang']."</td>";
echo "<td>" . $_SESSION['chart'][$indexo]['stokbarang']."</td>";
echo " <td><button id=".$indexo." name=\"button\">Edit</button></td>";
echo "</tr>";
}
echo "</table>";
}else
{
echo "Chart is still Empty";
}
}

echo "<table border=1 id=\"{$some_php_variable_with_an_id_value_in_it}\">"

first your code is too much line over there, that is no need to do such as that, it can be more simple to build some output with the same result, here may be could to help ya
function printdata(){
if(is_array($_SESSION['cart'])){
echo "Here's Your Chart ".count($_SESSION['cart'])."<br />";
echo '<table><tr>th>No</th><th>Nama Barang</th><th>Harga Barang</th><th>Stok Barang</th><th>Action</th></tr>';
for($indexo =0; $indexp < count($_SESSION['cart']); $indexo++){
echo "<tr>
<td id=".$indexo.">".$indexo."</td>
<td id=".$_SESSION['chart'][$indexo]['namabarang'].">".$_SESSION['chart'][$indexo]['namabarang']."<td>
<td id=".$_SESSION['chart'][$indexo]['hargabarang'].">".$_SESSION['chart'][$indexo]['hargabarang']."<td>
<td id="$_SESSION['chart'][$indexo]['stokbarang']">".$_SESSION['chart'][$indexo]['stokbarang']."<td>
<td><button id=".$indexo." name=\"button\">Edit</button></td>
</tr>";
}
}else{
echo "Chart is still Empty";
}
}
You can Change <td id=".$indexo."> to what ever you want to, seems like the other, id or rel will be the same implementation i guest

Related

How to make an dropdown menu to sort data from an database

I am working on an application to use on an website. The application let you add job offers to a database to post it on the website and on social media. I have done a lot already but now I am trying to make an sort menu. In my database I have 1 row that is called Status. The Status is an enum with the data Open and Closed so I would like to have a sort option. I would like to sort on Open, Closed and all.
I already have a table and I would like to see the changes on that existed table if that is possible but I have no idea how to do this. Can somebody help me with this? I am using HTML, PHP and as database PHPMyAdmin. Here is my code:
<!DOCTYPE HTML>
<html>
<head>
<style>
table {
border-collapse: collapse;
border: 1px solid black;
}
td {
border: 1px solid black;
width: 120px;
}
</style>
</head>
<body>
<h2>Jobs Overview (Admin)</h2>
<form action="/action_page.php">
<select id="sorting">
<option value="All" >All</option>
<option value="Open">Open</option>
<option value="Closed">Closed</option>
</select>
<br><br>
<?php
include 'Connection.php';
echo "<table>";
echo "<tr>";
echo "<th>" . "jid" . "</th>";
echo "<th>" . "role" . "</th>";
echo "<th>" . "type" . "</th>";
echo "<th>" . "availability" . "</th>";
echo "<th>" . "location" . "</th>";
echo "<th>" . "status" . "</th>";
echo "<th>" . "Verwijder?" . "</th>";
echo "</tr>";
$select = "SELECT * FROM test";
$result = mysql_query($select);
if($result) {
} else {
echo "Error! <br>";
echo mysql_error();
}
while($data = mysql_fetch_assoc ($result)) {
echo "<tr>";
echo "<td>" . $data["jid"] . "</td>";
echo "<td> <a href='jobsupdate.php?jid=" . $data["jid"] . "'>" . $data["role"] . " </a></td>";
echo "<td>" . $data["type"] . "</td>";
echo "<td>" . $data["availability"] . "</td>";
echo "<td>" . $data["location"] . "</td>";
echo "<td>" . $data["status"] . "</td>";
echo "<td> <center>Verwijder</center></td>";
}
echo "</tr>";
echo "</table>";
?>
<br><br>
Vacatures Toevoegen
</body>
</html>
Thanks in advance
You can add HTML class to open and closed jobs.
while($data = mysql_fetch_assoc ($result)) {
echo "<tr>";
echo "<td>" . $data["jid"] . "</td>";
echo "<td> <a href='jobsupdate.php?jid=" . $data["jid"] . "'>" . $data["role"] . " </a></td>";
echo "<td>" . $data["type"] . "</td>";
echo "<td>" . $data["availability"] . "</td>";
echo "<td>" . $data["location"] . "</td>";
if ($data["status"] == 0) {
echo "<td class='open'>" . $data["status"] . "</td>";
} else {
echo "<td class='closed'>" . $data["status"] . "</td>";
}
echo "<td> <center>Verwijder</center></td>";
}
echo "</tr>";
echo "</table>";
After, this you have to write a jquery script to hide the jobs .on change.
$('#sorting').on('change', function(){
var status = $('#sorting:selected').val();
if (status == "Open") {
$('.closed').hide();
} else if (status == "Closed") {
$('.open').hide();
} else if (status == "All"){
$('.closed').show();
$('.open').show();
}
});​
I hope this solves your problem.
You can make a Ajax post request on select change to get the records and put those records on success in your table.
More about how to do this with Jquery / Ajax here
More about Jquery on change here

Data from Database into the table (can't figure out how to do layout)

This is my code (horrible one):
<?php
include 'connect/con.php';
$result = mysqli_query($con,"SELECT id, vidTitle FROM newsvid");
$result1 = mysqli_query($con,"SELECT imgCover, vidSD FROM newsvid");
$result2 = mysqli_query($con,"SELECT published FROM newsvid");
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td >" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
mysqli_close($con);
?>
</body>
</html>
The question is how to show data from database in this layout:
--------------------------------
-id----------vidTitle-----------
--------------------------------
-imgCover------vidSD------------
--------------------------------
----------published-------------
So every time I will add more data , another block like I showed before will add up under existing one.
........................................................................................
There's no need to write 3 queries. You could do that with only one select, and then put all the echos inside a while. That way you're writing, it would run all the ids and titles first, then it would put a table after the table, with cover and vidSD.
Try to make a single query:
SELECT id, vidTitle, imgCover, vidSD, published FROM newsvid
That way you will have, on each row returned from database, all the information about the same row.
Now, running a while is the same as you're doing, just adapting some HTML:
echo "<table width='600' border='1'><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'>" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
You may want to order it too. Adding ORDER BY id DESC, would do that.

Associate Button Value with Query Results In Seperate Tables

I have this query:
$result = mysqli_query($con,"SELECT * FROM b_tasks_report WHERE TASK_ID=$taskid ORDER BY WEEK_ID");
$current_week_id = -1;
while($row = mysqli_fetch_array($result))
{
if($current_week_id != $row['WEEK_ID'])
{
if($current_week_id != - 1)
{
echo "</table>";
}
echo "<table>";
echo "<tr class='no-border'><td class='no-border'><div class='task-detail-title'>Week Number: " . $row['WEEK_ID'] . "</div></td></tr>";
echo "<tr>";
echo "<th width='100'>Day</th>";
echo "<th width='75'>Start</th>";
echo "<th width='75'>End</th>";
echo "<th width='100'>Billable Hours</th>";
echo "<th width='100'>Non Billable Hours</th>";
echo "</tr>";
$current_week_id = $row['WEEK_ID'];
}
echo "<tr>";
echo "<td class='tdclass'>" . $row['DAY'] . "</td>";
echo "<td class='tdclass'>" . $row['START'] . "</td>";
echo "<td class='tdclass'>" . $row['END'] . "</td>";
echo "<td class='tdclass'>" . $row['BILLABLE_HOURS'] . "</td>";
echo "<td class='tdclass'>" . $row['NON_BILLABLE_HOURS'] . "</td>";
echo "</tr>";
}
if($current_week_id != - 1)
{
echo "</table>";
}
This provides me with seperate tables for each week ID. However I am looking to get buttons to display below each table which are associated with the results above. Is it possible to add a button with the value of WEEK_ID. Currently if I add a button to the top and the bottom with value:
<input type='image' name='submit' src="image/button.jpg" value=" . $row['WEEK_ID'] . ">
It doesn't show the right ID for the top tables and it shows nothing for the bottom. I do understand why this is but is there anyway I can associate this button underneath the table?
Use this outside the PHP tag:
<input type="image" name="submit" src="image/button.jpg" value="<?php echo $row['WEEK_ID'] ?>">
Use this inside the PHP tag.
echo '<input type="image" name="submit" src="image/button.jpg" value="' . $row['WEEK_ID'] . '">';

If functions per $row in a table

I have a table that shows me data from a call flow.
I need the Data from this table to be manipulated per row in such a way that all the values of my table, which are being looked up from my DB, (which are now in code) will be translated into a text value. Let me show U and explain:
My Table:
<?php
include_once "Connect2Martijn1.php";
?>
<link rel="stylesheet" href="CSSMartijn1.css">
</link>
<head>
<meta charset="iso-8859-1">
<meta name="description"content="VoizXL ">
<meta name="keywords"content="VoizXL ">
<meta name="author"content="Kenn Lo-A-Tjong">
</meta>
<title>Call Flow</title>
</head>
<fieldset>
<article class="rondehoeken">
<header>
<div class="streep1"></div>
<div class="streep2"></div>
<div class="streep3"></div>
<div class="streep4"></div>
<div class="streep5"></div>
<h1 id="artikel-titel" >Call Flow</h1>
</header>
<div id="artikel-container">
<table class="table 1">
<thead>
<title>Call Flow</title>
<meta charset = "UTF-8" />
<style type = "text/css">
table, td, th {
border: 1px solid black;
}
</style>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost","root","","voizxl_wachtrij");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Callflow");
echo "<table border='0'>
<tr>
<th>Nummer</th>
<th>Naam</th>
<th>Status</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . $row['statusAnswered'] . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Example of (how I want to be) Translating the Data:
<?php
if ($row['statusAnswered'] ="NULL")
{
echo "Not Answered!";
}
else
{
echo "Answered!";
}
?>
What I want to Achieve is for eg. that the value in this table from $row['statusAnswered'] will be displayed in text as "Answered or Not Answered" if the Value of this row in the DB is NULL or Not...
How do I do this?
Right now I can only achieve to have 1 echo under the table saying Answered :S
No Idea how to put it per $row.
while($row = mysqli_fetch_array($result))
{
if (!isset($row['statusAnswered']))
{
$answered = "Not Answered!";
}
else
{
$answered = "Answered!";
}
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . $answered . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
if ($row['statusAnswered'] =="NULL"||$row['statusAnswered'] =="Null" || $row['statusAnswered'] =="null" || $row['statusAnswered'] =="")
{
echo "<td>Not Answered!</td>";
}
else
{
echo "<td>Answered!</td>";
}
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<table border='0'>
<tr>
<th>Nummer</th>
<th>Naam</th>
<th>Status</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . ($row['statusAnswered']=='NULL'?'Not Answered!':'Answered!') . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
You could use;
$checkstatus = if($row['statusAnswered'] = NULL) { echo "Not Answered!"; } else {echo "Answered!";};
then call that by replacing;
echo "<td>" . $row['statusAnswered'] . "</td>";
with;
echo "<td>{$checkstatus}</td>"

How to make PHP html table have color mismatch between rows?

I am learning PHP as I code. From an example on w3schools it showed using PHP and msql to display database results on a html table. My questions is, I now have too many rows and I could not make them have mismatch colors between rows. I've tried adding style span and font color after <td but it doesn't take it. the entire PHP just don't work if I do so.
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
The output of the code above will be:
Firstname Lastname
Glenn Quagmire
Peter Griffin
http://www.w3schools.com/php/php_mysql_select.asp
Not entirely sure what you mean by color mismatch. Assuming that you mean alternating row colors I'd do the following:
$odd = false;
while (...)
{
echo '<tr class="'.($odd ? "odd" : "even").'">';
...
echo "</tr>";
$odd = !$odd;
}
Now you have the tr element being of class odd or even alternatingly, and may specify some additional background color for one of them in your CSS, e.g.:
tr.odd { background-color: rgba(0, 0, 0, 0.05); }
Replace this:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
with this:
$i = 0;
while($row = mysql_fetch_array($result))
{
echo "<tr ". ($i % 2 == 0 ? 'style="background-color:grey;"' : '' .">";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$i++;
}
Every other row will have grey color.
Maybe you can use this approach with jquery
<script src="text/javascript">
$('#table tbody tr:odd').addClass('odd');
$('#table tbody tr:even').addClass('even');
</script>
and then add the styles
.odd { background-color: #color }
.even { background-color: #color }
$class = "even";
while($row = mysql_fetch_array($result))
{
if($class == "even")
{
echo "<tr class='$class'>";
$class = "odd";
}
else
{
echo "<tr class='$class'>";
$class = "even";
}
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
CSS
tr.even
{
background-color:blue;//Pick your own color
}
tr.odd
{
background-color:green;
}
Here is a list of color names. If you want a more detailed color choices, click here.
Use
$flag = 0;
while($row = mysql_fetch_array($result))
{
if ($flag%2 == 1)
echo "<tr bgcolor=#123345>";
else
echo echo "<tr bgcolor=#643235>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$flag = $flag +1;
}

Categories