Associate Button Value with Query Results In Seperate Tables - php

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'] . '">';

Related

How to echo text to cell, based on the value of another cell

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>";

Selection from option based on database values

I'm working on last part on my project, I'm building web-site, in this part, I want to display options of a job ( whether the job still in progress or Completed )
I gave my row in mysql enum values, "Completed","InProgress"
and when the student pick a job, the JobStatus will be "InProgress"
and the student can change this value from his JobLists page, when it's done, he can change it to Completed. and it will be changed in the Database
and this is my code trying to, in this Code, it shows me an Error on the Update Query
JobStatus = '".$_POST['JobStatus'] is not Defined ?? any one can help PLEASE Guys
<?php
//Connect to DB
include('CIEcon.php');
$sqlCommand ="SELECT Accounts.SSU , Jobs.JobName, Jobs.Description, Jobs.DueDate,Jobs.JobId, JobsLists.JobStatus FROM JobsLists,Jobs,Accounts WHERE Accounts.SSU = JobsLists.SSU AND Jobs.JobId = JobsLists.JobId And Accounts.SSU = '".$_SESSION['SSU']."' ";
$result = mysqli_query($dbCIE,$sqlCommand) or die(mysql_error());
echo "<form action='JobsLists.php' method='post'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='checkbox' name='JobId[]' value='". $row['JobId'] ."' /> </td>";
echo "<td align=center>" . $row['SSU'] . "</td>";
echo "<td align=center>" . $row['JobName'] . "</td>";
echo "<td align=center> " . $row['Description'] . "</td>";
echo "<td align=center>" . $row['DueDate'] . "</td>";
echo "<td align=center>" .
"<select>
<option name = JobStatus[".$row['JobId']."] value='InProgress' selected> In Progress </option>
<option name = JobStatus[".$row['JobId']."] value='Completed' > Completed </option>
</select>" . "</td>"; // need to be worked on..
echo "</tr>";
}
"</table>";
//Connect to DB
include('CIEcon.php');
// save the SSU for the current user to save the sata when insert jobs in jobslist
$SSU = $_SESSION['SSU'];
/////
//handle this when to save a status.
if( isset($_POST['save']) ){
if( empty($_POST['JobId']) || $_POST['JobId'] == 0 ){
echo"<h4> Status Wasn't Changed.. </h4>";
}else{
include('CIEcon.php'); //$dbCIE
foreach($_POST['JobId'] AS $i){
/// update JobsLists table with the new status..
$sqlUpdate = "UPDATE JobsLists SET JobStatus = '".$_POST['JobStatus'][$i]."' WHERE JobId = '" . $i . "'";
$resultUpdate = mysqli_query($dbCIE,$sqlUpdate) or die(mysqli_error($dbCIE));
}
// TEST ONLY ////////----------------------------------------////////////
if (mysqli_affected_rows($dbCIE) > 0) {
echo "<h4> You have successfully Saved your statuse </h4>";
}else{ echo "<h4> Error occurred </h4> "; }
////////----------------------------------------////////////
} // end of else, when user select something..
}
?>
It's because you haven't named the select box which you are trying to send values with.. HTML <option>s don't have a name, but only a value. it is this value which is the assigned to the name of the <select> in $_POST
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='checkbox' name='JobId[]' value='". $row['JobId'] ."' /> </td>";
echo "<td align=center>" . $row['SSU'] . "</td>";
echo "<td align=center>" . $row['JobName'] . "</td>";
echo "<td align=center> " . $row['Description'] . "</td>";
echo "<td align=center>" . $row['DueDate'] . "</td>";
echo "<td align=center>" .
echo "<select name='JobStatus[".$row['JobId']."]'>";
if($row['JobStatus'] == "InProgress"){
echo "<option value='InProgress' selected>In Progress</option>";
echo "<option value='Completed'>Completed</option>";
} else {
echo "<option value='InProgress'>In Progress</option>";
echo "<option value='Completed' selected> Completed </option>";
}
echo "</select>" . "</td>"; // need to be worked on..
echo "</tr>";
}
"</table>";

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.

Get table value html

I'm trying to update a mySQL table after a button click..The button click is not the problem but I wonder how I can get the klant_pk which is unique to update a certain record in mySQL. As you see I print out the mySql table at first. So is there anyone who know how I can get the according klant_pk after I click on a button in the table..
Thanks
$result = mysqli_query($con, "SELECT * FROM bestelling");
echo "Bestellingen";
echo "<table border='1' align='center'>
<tr>
<th>Bestelling_pk</th>
<th>Klant_pk</th>
<th>Product</th>
<th>Commentaar</th>
<th>Tijd</th>
<th> Voortgang </th>
<th> Status </th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['bestelling_pk'] . "</td>";
echo "<td>" . $row['klant_pk'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['opmerking'] . "</td>";
echo "<td>" . $row['tijd'] . "</td>";
echo "<td> <input type='button' value='In Wacht' onclick='return change(this);' />";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
while ($row = mysqli_fetch_array($result)) {
...
echo "<td id='klank_pk_".$row['klant_pk']."'>" . $row['klant_pk'] . "</td>";
...
echo "<td> <input type='button' value='In Wacht' onclick='change(getElementById('klank_pk_".$row['klant_pk']."').value);' />";
...
}

Finding query in substring instead of full text

I have created a search function. It can find address by the full text but how do I go about to make the query search by parts of the address ? For e.g. the full address is paya lebar Road Blk27, how do I make it so that the user can just type in paya and it would still show up ?
SearchForm
<h2>View Patient Records</h2>
<body>
<form action="display_patient.php" method="post">
<p>Select:
<select name="patient_var" >
<?php
$value = array(view_all, name, address);
foreach ($value as $option)
{
echo '<option value="'.$option.'"' . (isset($_POST['patient_var']) && $_POST['patient_var'] == $option ? ' selected' : '') . '>' . $option . '</option>';
}
?>
</select>
<input type="text" name="typed" value="" />
<input type ="submit" value="submit" />
</form>
</p>
<p>
<?php
if (isset($_POST['patient_var'])) {
$type = $_POST['typed'];
$select = $_POST['patient_var'];
if ($select == 'view_all') {
echo "<table border='1'>";
echo "<tr>\n";
echo "<th>ID</th>\n";
echo "<th>Patient Name</th>\n";
echo "<th>Age</th>\n";
echo "<th>NRIC</th>\n";
echo "<th>Birth Date</th>\n";
echo "<th>Medical Allergies</th>\n";
echo "<th>Medical History</th>\n";
echo "<th>Phone</th>\n";
echo "<th>Address</th>\n";
echo "<th>Doctor Assigned</th>\n";
echo "</tr>";
$pat_set = default_patient();
while ($mo = mysqli_fetch_array($pat_set)) {
echo "<tr>";
echo "<td>" . $mo['id'] . "</td>";
echo "<td>". $mo['name'] . "</td>";
echo "<td>". $mo['age'] . "</td>";
echo "<td>". $mo['nric'] . "</td>";
echo "<td>". $mo['birthdate'] . "</td>";
echo "<td>". $mo['medical_allergies'] . "</td>";
echo "<td>". $mo['medical_history'] . "</td>";
echo "<td>". $mo['phone'] . "</td>";
echo "<td>". $mo['address'] ."</td>";
echo "<td>". $mo['doctor_assigned'] . "</td>";
echo "</tr>";
}
}
else {
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>ID</th>\n";
echo "<th>Patient Name</th>\n";
echo "<th>Age</th>\n";
echo "<th>NRIC</th>\n";
echo "<th>Birth Date</th>\n";
echo "<th>Medical Allergies</th>\n";
echo "<th>Medical History</th>\n";
echo "<th>Phone</th>\n";
echo "<th>Address</th>\n";
echo "<th>Doctor Assigned</th>\n";
echo "</tr>";
$patients_set =
find_patients($select, $type);
while ($row = mysqli_fetch_array($patients_set))
{ echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>". $row['name'] . "</td>";
echo "<td>". $row['age'] . "</td>";
echo "<td>". $row['nric'] . "</td>";
echo "<td>". $row['birthdate'] . "</td>";
echo "<td>". $row['medical_allergies'] . "</td>";
echo "<td>". $row['medical_history'] . "</td>";
echo "<td>". $row['phone'] . "</td>";
echo "<td>". $row['address'] ."</td>";
echo "<td>". $row['doctor_assigned'] . "</td>";
echo "</tr>"; }
}
}//end of if post submit
?>
</p>
In Mysql For searching whether a column has specific string use LIKE
Example
SELECT * FROM table WHERE column LIKE '%somestring%';
In your case just try this
$typed = $_POST['typed'];
and make Mysql query like this
$query = "select * from table where Address LIKE '%".$typed."%' ";
Use LIKE '%$searchParam%' instead of = $searchParam
You forgot to post your function: find_patients($select, $type);
But I guess you need a partial string query for an address varchar field which looks something like this:
select * from MyTable where myColumn like '%myPartialString%';
This type of query will return all rows that have "MyPartialString" within.

Categories