How to show array name after echo? - php

I have an array of options that is used in a form's drop down select input.
DATA.php file that holds the array info...
'options' => array("Car", "SUV", "Pickup", "Van", "Bus", "Motorcycle");
Edit page that displays the form and calls the options for the drop down input..
echo '<option '.$selected.' value="'.$optionValue.'">'.$optionLabel.'</option>';
when the form is saved the array option position value is saved to my database. I have a table on another page that echos some of the information the user submitted. But because the information is being pulled from the database as is, the information shows up as either, 0, 1, 2, 3, etc. which is the arrays position value (example: 0=car, 1=Suv, 2=pickup, etc.)
this displays the table...
<tr>
<td><span class="nobr"><?php echo $_sublogin->getData('vehicle_type') ?>
</span></td>
</tr>
How do i display the information back, so the table knows if the value is 1 echo SUV, etc.
I'm pretty new at php and i feel this is something i should know how to do but i dont. please help!
thank you.

That is probably because $optionValue contains 0,1,2... Page submits value part of the dropdown list (value="'.$optionValue.'"). I think $optionLabel has the values car, SUV... etc. So what you can do is replace $optionValue with $optionLabel.
echo '<option '.$selected.' value="'.$optionLabel.'">'.$optionLabel.'</option>';
That will save the label value to the database.

if i understood your problem then you can use if/else condition for showing name against their value. like this-
<?php
if($_sublogin->getData('vehicle_type') == 1):
?>
<tr>
<td><span class="nobr"> SUB </span></td>
</tr>
<?php
elseif($_sublogin->getData('vehicle_type') == 2):
?>
<tr>
<td><span class="nobr"> CAR </span></td>
</tr>
.
.
.
.
<?php endif;?>

are you looking for this:
$arr['options']=array("Car", "SUV", "Pickup", "Van", "Bus", "Motorcycle");
echo $arr['options'][1];
//output: SUV

Related

Display message in table if cell content is empty

I am trying to echo values from mysql database into a table but some values may be empty and I don't want the cell to output empty. Is there a way for me to show a message e.g "N/A" if a particular value is empty in the database?
<table><tr><td>Friends Name:</td><td><b>".$row['first_name']." ".$row['last_name']."</b></td></tr></table>
Something like
<table>
<tr>
<td>Friends Name:</td>
<?php if($row['first_name']): ?>
<td><b>".$row['first_name']." ".$row['last_name']."</b></td>
<?php else: ?>
<td><b>N/A</b></td>
<?php endif; ?>
</tr>
</table>

displaying table field names dynamically with cake php

I am trying to display a list of all entries in the table people. I have the list of entries generating correctly as a html table but I want to ad a row at the top of the table with the table field names. I could do this manually but figure there must be a way to do it dynamically in case I add or remove fields later on.
here is the controller function
public function peopleDisplay() {
$this->set('people', $this->people->find('all'));
}
Are the field names already in the array that generates? If so how do I reference them? If not how do I get them to be?
Here is the view so far
<table>
<tr>
***were I want the row of field names to go***
</tr>
<?php foreach($people as $people): ?>
<tr>
<td>
<?php echo $people['people']['id'] ?>
</td>
<td>
<?php echo $people['people']['firstName'] ?>
</td>
<td>
<?php echo $people['people']['secondName'] ?>
</td>
</tr>
<?php endforeach; ?>
</table>
The column names are the keys in your array. For instance, 'id', 'firstName', and 'secondName' are the column names.
Another way to approach would be to get the column names as a separate array in your controller and then output them in your view.
$people = $this->People->find('all');
$colNames = array_keys($this->People->getColumnTypes());
$this->set(compact('colNames', 'people'));
Then in your view:
foreach($colNames as $col){ //Output column names }
foreach($people as $person){ //Output people }

passing a php array item into a javascript popup window to be edited and updated in mysql db

I am creating a diabetes management system for a university project. One of the features of this system is for the patient to be able to send latest glucose readings and for the nurse to be able to login and comment on those readings.
I have been able to code the patient feature, however I wish to add a comment button in the comments column, which when clicked, brings up a popup window or a textbox for the nurse to be able to comment on that specific record. If a comment has not already been entered, an empty box should come up, however if there has been a previously entered comment, it should be displayed in the box to be updated and sent back to the mysql database. I would like to ask if someone could give me a way to include this comment box and code for the existing value to be displayed in the box and if there is no existing comment, then a new comment can be entered and stored in the database.
Below is my php code.
<?php//run query
$result = mysql_query($GetReadings);
?>
<table>
<tr>
<th>Date</th>
<th>Time</th>
<th>Glucose Level</th>
<th>SBP</th>
<th>DBP</th>
<th>Comments</th>
</tr>
<?php
//display results
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<tr>
<td><?php echo $row["Date"]; ?> </td>
<td><?php echo $row["Time"]; ?> </td>
<td><?php echo $row["GlucoseLevel"]; ?> </td>
<td><?php echo $row["SBP"]; ?> </td>
<td><?php echo $row["DBP"]; ?> </td>
<td><?php echo $row["Comments"];
<?php
//if statement to add comment link if user is a nurse
if ($_SESSION['User_level'] == 2)
{
//code for comments
}
?> </td>
</tr>
<?php
//end of while loop
}
?>
Hope I haven't missed out any crucial information.
Use the javascript function :
window.open(URL, windowName[, windowFeatures])
Where, URL - desired URL to display a page containing Textbox, use any window name you want.
Echo <a> or button with a onclick event eg:
Add Comment
Edit
The most basic way to achieve is, echo a <div></div> containing the past comments, the text box for new comments and Send/Cancel buttons. The trick is to set the display:none style property of that div. You the following code a guideline :
Echo the following code if the User has right user level.
Show Comments
<div id="comment-<?php echo $row['id']?>" style="display:none">
//display previous comments
<form method="post" action="addComment.php?id=<?php echo $row['id']?>">
<textarea name="comment"></textarea>
<input type="submit" value="Add Comment" /><input type="button" onclick="hideComment('<?php echo $row['id']?>')">
</form>
</div>
<script type="text/javascript">
function hideComment(id) {
document.getElementById('comment-' + id).style.display = 'none';
}
function showComment(id) {
document.getElementById('comment-' + id).style.display = 'block';
}
</script>

Click name from one mysql table to show report from another table

I have a table with the following fields:
email - name - username - userid
currently the data in this table is pulled into a html table.
In a seperate table i have all the user's data / information.
What i would like to do is click on a name from the first table (consisting of email - name - username)
And have that users information shown on its own like a report generation.
Both the tables have the same unique userid's applied so could someone enlighten me as to the best way to do this?
Thanks.
Surround the name with an anchor tag that has the id as some parameter.
I'd do it with 2 templates where one lists all the users (userList.php) and the other one shows detailed information about a user (userInformation.php).
userList.php:
<table>
<tr>
<th>
<a href="userInformation.php?id=<?php echo $user->id;?>">
<?php echo $user->username;?>
</a>
</th>
<td><?php echo $user->email;?></td>
<td><?php echo $user->propN;?></td>
</tr>
...
...
</table>
userInformation.php:
<?php
$userId = $_POST['id'];
$user = someFunctionForGettingTheUserPerhaps($userId);
?>
<table>
<tr>
<th><?php echo $user->username;?></th>
<td><?php echo $user->email;?></td>
<td><?php echo $user->password;?></td>
<td><?php echo $user->name;?></td>
<td><?php echo $user->propN;?></td>
...
...
</tr>
</table>
EDIT: Replaced '.' with '->' since the latter is the property accessor notation in PHP.
Set the onclick attribute of each table row to
echo('<tr onclick="location.href=\'userdet.php?id='.$row['userid'].'\'">
Where userdet.php is a page that puts the information you want into a HTML table.
edit
You could also try putting the data in like this http://www.jsfiddle.net/dduncan/UuA9E/
although that could get slow if you have a massive users table. (click the table row)

Submitting values of "jQuery Editable Invoice" into a MySQL DB using PHP

I am using this fantastic example of a jQuery editable invoice as a template for creating dynamic invoices for my users.
It's working quite well and I am successfully generating the items but I now need to save the values entered into the various text fields and enter them into the MySQL database.
I am confident in doing the MySQL entering with PHP but what makes this trickier is that the amount of 'invoice items' is completely dynamic and I am unsure how I can get PHP to 'check' through the pages text fields and find new ones, group them and then add them to my DB.
Here is an example of my code that I am using to generate the items:
<?php if($invoice_items->result_array()) { ?>
<?php foreach($invoice_items->result_array() as $invoice_Row): ?>
<tr class="item-row">
<td class="item-name">
<div class="delete-wpr">
<textarea><?php echo $invoice_Row['item_name']; ?> Facility Booking</textarea>
<a class="delete" href="javascript:;" title="Remove row">X</a>
</div>
</td>
<td class="description">
<textarea><?php echo $invoice_Row['description']; ?></textarea>
</td>
<td><textarea class="cost">$<?php echo $invoice_Row['hourly_cost']; ?>.00</textarea></td>
<td><textarea class="qty"><?php echo $total_time_hours; ?></textarea></td>
<td><span class="price">$<?php $unit_total = $invoice_Row['hourly_cost']* $total_time_hours; echo $unit_total;?>.00</span></td>
</tr>
<?php endforeach; ?>
<?php } ?>
I am thinking that I need to perhaps generate unique ID's for each invoice items text field, ie item-1-desc, item-1-cost etc, but that involves writing javascript which I know almost nothing about. Also I would still have to get PHP to loop through the ID's somehow until it reached the end...
If anyone has attempted something similar before or you can see a solution to my problem I would greatly appreciate your help.
Thanks,
Tim
Use the php form array syntax name="item-desc[<?php echo $id?>]"
You can then iterate them on the backend with foreach to store the data. You have the id's as keys to the arrays so it should be fairly trivial to update the db.

Categories