So this is a quick question I have about my php page. I wont put the code up because it would turn into solving the syntax. I was looking for the reason this happens.
This being, I have a .php page and it loads and about half way down and there is a short bit of php code. And in it, it echos to add a few items to a database. But then none of the rest of the html page loads.
Its a php type file.
Actually here is the part within the html.
<select id="price_select" name="priceBox">
<?php
...
//connects to database in code not shown
$result=mysqli_query("SELECT * FROM $tablename")or die(mysql_error());
$count=0;
while($row=mysqli_fetch_array($result)) {
$price = $row['price'];
if($price != NULL){
($count = $count+1);
}
echo "$(\"#price_select\").append('<option>" . $price . "</option>');";
}
if($count==0) {
echo "$(\"#price_select\").append('<option>Out of Stock</option>');";
}
?>
</select>
You have error on the following line:
$result=mysqli_query("SELECT * FROM $tablename")or die(mysql_error());
You must have similar:
$result=mysqli_query($link,"SELECT * FROM $tablename")or die(mysqli_error($link));
You php code should be similar:
$result=mysqli_query($link,"SELECT * FROM $tablename")or die(mysqli_error($link));
$count=0;
while($row=mysqli_fetch_array($result)) {
$price = $row['price'];
if($price != NULL){
($count = $count+1);
echo "<option> $price </option>";
}
}
if ($count === 0)
{
echo "<option>Out of Stock</option>";
}
change your code like below.
$html = '';
while($row=mysqli_fetch_array($result)) {
$price = $row['price'];
if($price != NULL){
($count = $count+1);
}
$html .= '<option>" . $price . "</option>';
}
if($count==0) {
echo '<option>Out of Stock</option>';
} else {
echo $html;
}
Modify the code adding <script>-tag as follows:
<select id="price_select" name="priceBox">
<?php
...
//connects to database in code not shown
$result=mysqli_query("SELECT * FROM $tablename")or die(mysql_error());
$count=0;
while($row=mysqli_fetch_array($result)) {
$price = $row['price'];
if($price != NULL){
($count = $count+1);
}
echo "<script>$(\"#price_select\").append('<option>" . $price . "</option>');</script>";
}
if($count==0) {
echo "<script>$(\"#price_select\").append('<option>Out of Stock</option>');</script>";
}
?>
</select>
Related
I already try many ways but the value didn't show in dropdown list
Here, this is my code. can you suggest me anything that i was wrong
<?php
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
$row = mysqli_fetch_assoc( $result );
$pos = 0;
echo "<select name=Pname >";
while($pos <= count ($row)){
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
$pos++;
}
echo "</select>";?>
And i write as .php file. Thanks for your help.
Try this out:
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output .= '<option value="' . $project_no . '">' . $project_name . '</option>";
}
}
Then inside of your HTML, print your $output variable inside of your <select> element:
<select>
<?php
print("$output");
?>
</select>
It should print all options for every row that you have requested from the database.
Hope this helps :)
Try this:
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
echo "<select name=Pname >";
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
}
echo "</select>";
}
This is the result code that i can run it. I put this code in a form code of html
$result = mysqli_query($con,"SELECT * FROM project"); ?>
<?php
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
echo " <select name = Pname>";
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output = "<option value=" . $project_no . "> ". $project_name ." </option>";
print("$output");
}
echo " </select>";
}
?>
Thank you every one for helping me ^^
When I try to do simple code like this everything works fine:
<?php
require 'connect.php';
if($result = $con->query("SELECT * FROM table")) {
if($row = $result->fetch_object()) {
echo $row->imgName, ' ',$row->divId, '<br>';
}
}
?>
But inserting an image is not working for some reason:
<?php
require 'connect.php';
if($result = $con->query("SELECT * FROM table")) {
if($row = $result->fetch_object()) {
echo "<img src='img/".$row['imgName']."' />";
}
}
?>
Can you help me out, please!
Change $row['imgName'] to $row->imgName
I have big problem with my code. In loop while working only first argument if. Else doesn't work... Here is a code :
$sql="SELECT * FROM house1 WHERE week = '".$q."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
if ($row['house'] == $q)
{
echo '<img src="house2.gif"/>';
}
else
{
echo '<img src="house1.gif"/>';
}
}
I'm trying to read a .CSV file and print it in a table format in HTML. At the end of the page is a comments text field where comments get submitted and saved in the database.
When I tested the code below locally it works fine. When I tried to run it on the linux server, it prints out fine when first opened, but when I press submit to save a comment, the page refreshes and the table does not print. Giving an "Invalid argument supplied for foreach()" error. (Note: this doesn't happen locally, i can submit all I want and it does not return an error.)
I've searched on stackoverflow and it seems that most of these problems are related to declaring the variable as an array. However, it seems odd to me as the code works fine the first time with no error, but once I submit it returns an error.
UPDATE: full code for file posted below.
<script>
window.onunload = refreshParent;
function refreshParent() {
window.opener.location.reload();
}
</script>
<?php
//---------------------------------Head/BG---------------------------------------
//Request Case ID
$case = "";
if(isset($_REQUEST['case'])) {
$case = $_REQUEST['case'];
}
$patientID = "";
if(isset($_REQUEST['patient'])) {
$patientID = $_REQUEST['patient'];
}
//Include basic functions to allow connection to SQL db.
include("generic.php");
//Include css and header information.
$printTitle = "Volume Report for Case ".$case."";
$printHeader = "Volume Report for Case ".$case."";
$printFooter = "";
$printBreadcrumb = "";
include("header.php");
//submit tableStatus update
if(isset($_REQUEST['submit'])) {
saveTableStatus($case);
}
//-----------------------------Start of Content----------------------------------
showStatusComment($case);
printVolumeTable($case,$patientID);
tableStatus($case);
//---------------------------End of Content--------------------------------------
//---------------------------Functions Definitions-------------------------------
//print report.csv Table
function printVolumeTable($case,$patientID){
echo "<html><body><table border='1'>\n\n";
$f = fopen("analyze/".$case."/".$patientID."/report.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "<tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
}
function showStatusComment($case) {
$connection = getMySqlConnection();
$sql = "SELECT p.STATUS_NAME, c.volume_comments FROM cases c, primary_status_lookup as p WHERE c.volume_status=p.STATUS_ID and c.caseid='".$case."'";
$result = mysql_query($sql, $connection) or die(mysql_error());
if($result!== FALSE){
while ($record = mysql_fetch_row($result)) {
$status=$record[0];
$comments=$record[1];
if($status == 'Clear Status') {$status = 'None'; $comments = 'None';}
print("<p><b>Table Status: </b>".$status." / <b>Comments: </b>".$comments."</p>");
}
}
}
//Status & Comments
function tableStatus($case) {
$connection = getMySqlConnection();
$sql = "SELECT volume_status, volume_comments FROM cases WHERE caseid='".$case."'";
$result = mysql_query($sql, $connection) or die(mysql_error());
if($result!== FALSE){
while ($record = mysql_fetch_row($result)) {
$status=$record[0];
$comments=$record[1];
print("<form><p>");
showStatusComment($case);
statusDropdown($case,$status);
print("<input type=hidden name='case' value='".$case."'/>");
print(" <label><b>Comments:</b><textarea name='comments' cols=70 rows=2 >".$comments."</textarea></label><br/><br/>");
print("<input type='submit' name='submit' value='Submit'/><INPUT type='button' value='Close Window' onClick='window.close()'></form>");
}
}
}
//Status Dropdown
function statusDropdown($case,$status){
print("<b>Status:</b>");
$dropdown = "<select name = 'status'><option selected='selected' value=NULL>--Select Status--</option>";
$connection = getMySqlConnection();
$sql = "SELECT STATUS_ID, STATUS_NAME FROM primary_status_lookup ORDER BY STATUS_ID ASC";
$result = mysql_query($sql, $connection) or die(mysql_error());
while($record=mysql_fetch_array($result)){
if ($status == '') {
$dropdown .= "<option value = '{$record['STATUS_ID']}'> {$record['STATUS_NAME']}</option>";
} else if (($status == $record['STATUS_ID']) && ($status == '99')) {
$dropdown .= "<option value = '{$record['STATUS_ID']}'> {$record['STATUS_NAME']}</option>";
} else if ($status == $record['STATUS_ID']) {
$dropdown .= "<option value = '{$record['STATUS_ID']}' selected='selected'> {$record['STATUS_NAME']}</option>";
} else {
$dropdown .= "<option value = '{$record['STATUS_ID']}'> {$record['STATUS_NAME']}</option>";
}
}
$dropdown .="</select>";
echo $dropdown;
}
function saveTableStatus($case)
{
//retrieve selected status
$status = '';
if(isset($_REQUEST['status'])) {
$status = $_REQUEST['status'];
}
//retrieve typed comments
if(isset($_REQUEST['comments'])) {
$comments = $_REQUEST['comments'];
}
if($status=='NULL') {
print("<p class='error'>No status selected, please select a status and try again.</p>");
}
else if (($status!=='NULL')){
$connection = getMySqlConnection();
mysql_query("START TRANSACTION", $connection);
if ($status =='99') {$comments = '';}
$result= mysql_query("Update cases Set volume_status=".$status.", volume_comments ='".mysql_real_escape_string($comments)."' Where caseid='".mysql_real_escape_string($case)."'", $connection);
if($result) {
mysql_query("COMMIT", $connection);
print("<p class='saved'>Table Status Updated!</p>");
} else {
mysql_query("ROLLBACK", $connection);
}
mysql_close($connection);
}
}
?>
If you form, and the script that takes the posted form are not on the same path, then your
$f = fopen("analyze/".$case."/".$patientID."/report.csv", "r");
will not open the same file.
Edit -
Okay I think your problem is your $case variable. If there is no request, the $case is blank (""). So the above line will open "analyze///report.csv" As you can see depending on this code
$case = "";
if(isset($_REQUEST['case'])) {
$case = $_REQUEST['case'];
}
I have a little question for you!
I make a selection from mysql my buttons for example:
$query = mysql_query("select * from navigation");
while ($row = mysql_fetch_assoc($query)) {
echo '$row['name']';
}
so I call all the navigation from mysql and I have a class in css which is called as .active, this class make a active button when I click it, but how can I make the first button active?
$query = mysql_query("select * from navigation");
while ($row = mysql_fetch_assoc($query)) {
if ($row['id'] == $_GET['id'])) {
echo '<a class="active" href="?id=$row['id']">$row['name']</a>';
} else {
echo '$row['name']';
}
}
Try this.
<?PHP
$query = mysql_query("select * from navigation");
$first = true;
while($row = mysql_fetch_assoc($query)) {
if ((!array_key_exists('id', $_GET) && $first) || $row['id'] == $_GET['id']) {
$extra = 'class="active"';
$first = false;
} else
$extra = '';
echo "<a $extra href=\"?id={$row['id']}\">{$row['name']}</a>";
}
?>