i want to check if a checkbox is checked through a mysql database request.
it should be something like this (just a concept code, what of course isnt working)
that should work probably with ajax, becouse i dont want to reload all the time:
$ergebnis = $mysqli->query("SELECT text,status FROM checkboxes where id=1 ;");
while($zeile = $ergebnis->fetch_array()) {
echo "<input type=\"checkbox\";
if ({$zeile['status']} == "true") {checked=\"checked\"}\n ;
echo " name=\"feld\" class=\"checkIt\"/>";
echo " {$zeile['text']}\n";
echo "";
}
?>
i have got 3 fields in the database. One text field where the text next to the checkbox shows up, a status field, where the script can see if something is "true" or "false" an a auto incrementation id.
I hope you can help me
write this code
$ergebnis = $mysqli->query("SELECT text,status FROM checkboxes where id=1 ;");
while($zeile = $ergebnis->fetch_array()) {
$text = "";
$text .= "<input type=\"checkbox\"";
if ({$zeile['status']} == "true") { $text .= " checked=\"checked\""; }
$text .= " name=\"feld\" class=\"checkIt\"/>";
$text .= "{$zeile['text']}";
echo $text;
//echo "";
}
There are so many errors was present in your code.
$ergebnis = $mysqli->query("SELECT text,status FROM checkboxes where id=1 ;");
while($zeile = $ergebnis->fetch_array()) {
echo '<input type="checkbox"';
if ({$zeile['status']} == "true") { echo ' checked="checked"'; }
echo ' name="feld" class="checkIt" />';
echo $zeile['text'];
}
?>
Related
Basically I'm doing digital signage and I'm trying to get names to be pulled from a MySQL database to a PHP page. Right now its all centered in one column, but I want the results to be in two columns side by side. How can I do this?
$sql = "SELECT * FROM donor WHERE DonationAmount = 5000 AND Category = '1' or DonationAmount = 5000 AND Category IS NULL ORDER BY LastName ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
// test if the DisplayName field is empty or not
if(empty($row['DisplayName']))
{
// it's empty!
if(empty($row['FirstName'])){
echo $row['LastName']. "<br>";
}
else{
echo $row["LastName"]. ", " . $row["FirstName"]. "<br>";
}
}else{
// Do stuff with the field
echo $row["DisplayName"]. "<br>";
}
}
} else {
}
Basically I want this data to be spread across two columns instead of 1 single page.
output the strings like this:
echo "<span style=\"width:50%;float:left;\">".$row['LastName']."</span>";
do not forget to remove <br /> from each output
You can use tables, and count the rows to determine if you need to start a new table row.
$i = 0;
$total_rows = $result->num_rows;
echo "<table><tr>";
while($row = mysqli_fetch_assoc($result)) {
// test if the DisplayName field is empty or not
echo "<td>";
if(empty($row['DisplayName']))
{
// it's empty!
if(empty($row['FirstName'])){
echo $row['LastName'];
}
else{
echo $row["LastName"]. ", " . $row["FirstName"];
}
}else{
// Do stuff with the field
echo $row["DisplayName"]. "";
}
echo "</td>";
$i++;
if($i % 2 == 0 && $i != $total_rows) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
if your content is in <div id="myDiv"> use this JS function and call it after the content loads
function splitValues() {
var output = "";
var names = document.getElementById('myDiv').innerHTML.split("<br>");
for(var i in names) {
output += "<span style=\"width:50%;float:left;display:inline-block;text-align:center;\">"+names[i]+"</span>";
}
document.getElementById('myDiv').innerHTML = output;
}
Ive been searching alot lately but couldnt come up for a solution about this problem of looping...
i am currently workin on a page that would display transactions from database and would let you pay using a button.
i made a table and put a button in the last column of every row using while($row = mysql_fetch_array($query)).
i made use of isset that everytime the button is clicked some session will store corresponding to the values of the row...but the problem is whichever button i clicked whether its in the first, second, or third row the values that i get comes from the last row.
Is there a way to get the value of the variables of each row? here are the codes:
$qry1=mysql_query("SELECT * FROM request WHERE client_id ='".$_SESSION['client_id']."'");
$check = mysql_num_rows($qry1);
if ($check == 0) { echo "There are no recent transactions to display."; }
else{
$table = "<table align= 'center' border='1' bordercolor='#ccc' cellpadding='20'>";
$table .= "<tr><th>REQUEST #</th><th>DATE</th><th>TIME</th><th>EVENT</th><th>PRICE</th><th>STATUS</th><th></th></tr>";
while($row = mysql_fetch_array($query)){
print_r($row);
$rid = $row['request_id'];
$reid = $row['event_id'];
$raid = $row['addons_id'];
$reprice = $row['request_price'];
$redate = $row['event_date'];
$retime = $row['event_time'];
$restatus = $row['event_status'];
if($reid==1 || $reid==2 || $reid==3){
$debut="Standard Debut";
}
else { $debut="Kids Party";
}
$table .= "<tr><td align = center># ".$rid."</td><td>".$redate."</td><td>".$retime."</td><td align= center>".$debut."</td><td align=center>P".$reprice.".00</td><td align=center>".$restatus."</td>";
if($restatus=='PENDING')
{
$table .= "<td align=center>";
$table .= "<form method='post'>";
$table .= "<input type='submit' name='btnSubmit' id='btnSubmit' value='Pay' tabindex='0' />
</form>
</td>";
include "../pages/my_reqsubmit.php";
}
else
{
$table .= "<td align=center>--</td>";
}
$table .= "</tr>";
}
$table .= "</table>";
echo $table;
code for my_reqsubmit.php :
if(isset($_POST["btnSubmit"]))
{
include("../pages/config.php");
$URL= "../request/view.php";
$_SESSION['RequestUser']="asd";
$_SESSION['dtRequest'] = $redate;
$_SESSION['tmRequest'] = $retime;
$_SESSION['etype'] = $debut;
$_SESSION['addons_id'] = $raid;
if($reid==1){
$_SESSION['epack']= "Package A";
}
else if($reid==2){
$_SESSION['epack']= "Package B";
}
else if($reid==3){
$_SESSION['epack']= "Package C";
}
else if($reid==4){
$_SESSION['epack']= "Package A";
}
else if($reid==5){
$_SESSION['epack']= "Package B";
}
else if($reid==6){
$_SESSION['epack']= "Package C";
}
$qrry2 = mysql_query("SELECT * FROM addons WHERE addons_id =$raid");
while($row = mysql_fetch_array($qrry2)){
$_SESSION['pservice'] = $row['addons_ps'];
$_SESSION['pbooth'] = $row['addons_pb'];
$_SESSION['mbar'] = $row['addons_mb'];
$_SESSION['phprice'] = 3500;
$_SESSION['pbprice'] = 5000;
$_SESSION['mbprice'] = 11000;
}
$qrry3 = mysql_query("SELECT * FROM event WHERE event_id =$reid");
while($row = mysql_fetch_array($qrry3)){
$_SESSION['eprice'] = $row['event_price'];
}
$_SESSION['price'] = $reprice;
header ("Location: $URL");
}
?>
Please help me...id really appreciate it!
You need to include SOMETHING in your button to link that row's button with the matching DB record. I'll use <a> tags instead, but the EXACT same concept is used for buttons:
Click here for record #1
^---the ID of the record that this button/link would use
Click here for record #2
Click here for record #3
Hi I'm working on making a form to connect to a database. It works good so far. It displays information. However I want to include more then one field.
Like if I were only to enter "Oakland" in the "Location" form.
I think the isset field is the part I'm getting stuck at. I tried include '&!' or '||' to no effect. Or maybe its my html. I tried to include some code in the "isset" and it came up a "No data retrieved from server" error came up on browser. Because I only entered one field. I want to do it based on where the user enters information. BTW I do have a include in it to include the other files. Its just a snippet. It just I think that this is where my errors at.
Here is my html code. If you guys could help me figure out how to do this.
echo "<html>";
echo "<head>";
echo "<title>Your Title Here</title>";
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />";
echo "</head>";
echo "<body onLoad=\"self.focus();document.searchform.search.focus()\">";
echo "<center>";
echo "<br /><form name=\"searchform\" method=\"GET\" action=\"search.php\">";
echo "<input type=\"text\" name=\"search\" size=\"20\" TABINDEX=\"1\" />";
echo "<input type=\"text\" name=\"Institution\" size=\"20\" TABINDEX=\"1\" />";
echo "<input type=\"text\" name=\"Location\" size=\"20\" TABINDEX=\"1\" />";
echo "<input type=\"text\" name=\"ProjectNotes\" size=\"20\" TABINDEX=\"1\" />";
echo "<input type=\"text\" name=\"TotalFunding\" size=\"20\" TABINDEX=\"1\" />";
echo "<input type=\"text\" name=\"ActiveYear\" size=\"20\" TABINDEX=\"1\" />";
echo " <input type=\"submit\" value=\"Search\" />";
echo "</form>";
//search variable = data in search box or url
if (isset($_GET['search'])) {
$search = $_GET['search'];
Investigator($search);
}
Investigator function
function Investigator($search)
{
$search = trim($search);
$search = preg_replace('/\s+/', ' ', $search);
//seperate multiple keywords into array space delimited
$keywords = explode(" ", $search);
//Clean empty arrays so they don't get every row as result
$keywords = array_diff($keywords, array(
""
));
//Set the MySQL query
if ($search == NULL or $search == '%') {
} else {
for ($i = 0; $i < count($keywords); $i++) {
$query = "SELECT * FROM Studies " . "WHERE Investigator LIKE '%$keywords[$i]%'" . " OR Location LIKE '%$keywords[$i]%'" . " OR TotalFundingAmount LIKE '%$keywords[$i]%'" . " OR Institution LIKE '%$keywords[$i]%'" . " ORDER BY Location";
}
//Store the results in a variable or die if query fails
$result = mysql_query($query) or die(mysql_error());
}
if ($search == NULL or $search == '%') {
} else {
//Count the rows retrived
$count = mysql_num_rows($result);
echo $count;
}
//If search variable is null do nothing, else print it.
if ($search == NULL) {
} else {
echo "You searched for <b><FONT COLOR=\"blue\">";
foreach ($keywords as $value) {
print "$value ";
}
echo "</font></b>";
}
echo "<p> </p><br />";
echo "</center>";
//If users doesn't enter anything into search box tell them to.
if ($search == NULL) {
echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>";
} elseif ($search == '%') {
echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>";
//If no results are returned print it
} elseif ($count <= 0) {
echo "<center><b><FONT COLOR=\"red\">Your query returned no results from the database.</font></b><br /></center>";
//ELSE print the data in a table
} else {
//Table header
echo "<center>";
echo "</center>";
//Colors for alternation of row color on results table
$color1 = "#d5d5d5";
$color2 = "#e5e5e5";
//While there are rows, print it.
while ($row = mysql_fetch_array($result)) {
//Row color alternates for each row
$row_color = ($row_count % 2) ? $color1 : $color2;
//table background color = row_color variable
echo "<center><table bgcolor=" . $row_color . ">";
echo "<tr>";
echo "<td>" . $row['Investigator'] . "</td>";
echo "<td>" . $row['TotalFundingAmount'] . "</td>";
echo "<td>" . $row['Institution'] . "</td>";
echo "</tr>";
echo "</table></center>";
$row_count++;
//end while
}
//end if
}
echo "</body>";
echo "</html>";
if ($search == NULL or $search == '%') {
} else {
//clear memory
mysql_free_result($result);
}
}
?>
isset() only checks to see if a variable is defined. empty() may be what you want to use in this case, because it will specifically check to see if the form input is empty.
Using this you can then construct a query which will be executed--in fact, it might eliminate the need for a separate function. I haven't looked too much at the function code you posted. But it would go something like this:
Say you have input fields x, y, and z. You want to be able to run a query that returns all the information matching any of these fields that are filled out. The script, then, would run the same pattern of code for each input, with $datax being the data from the form.
if (empty($datax) == false) //If the data is NOT empty...
{
$stringx = "column = {$datax}"; //Assign a chunk of a mySQL query requesting matching info to a string.
}
else //If the data IS empty...
{
$stringx = "1"; //Create the same variable, but make it 1. You'll see why.
}
//Repeat the above for $datay and $stringy, $dataz and $stringz, etc. Next is another if to make sure the entire form isn't left blank.
if (empty($datax) == true && empty($datay) == true && empty($dataz) == true)
{
//Code to return an "Empty form!" error
}
else
{
$query = "SELECT * FROM table WHERE " . $stringx . " AND " . $stringy . " AND " . $stringz;
//Continue to run query
}
What this does is construct a query from the data results. If, for example, a user searches for USA, a blank field, and industrial, (with fields being location, weather, and type respectively--again, all just an example) the query looks like:
SELECT * FROM table WHERE location = 'USA' AND 1 AND type = 'industrial'
Any fields left blank result in an AND 1 statement inserted, not affecting the query. Hope that clears things up a little, let me know if I can make further edits to help.
Am new to php... I have been battling on my dynamic checkboxes in such a way that if none is checked the form is return, also I need to retain what was checked when the form postback due to other invalid inputs.
$result = mysql_query("SELECT * FROM course") or die(mysql_error());
if ($result)
{
while ($row = mysql_fetch_array($result)){
if (isset($_POST['courses']) and $_POST['courses'] == $row['cid']) {echo $row['cid'];}
print "<input type=\"checkbox\" name=\"courses[]\" value=\"$row[cid]\">$row[cname]\n";
}
}
Help needed purely on php codes. Thanks in advance
Do this where the checkbox appears in the HTML on your php page:
<?php
$checked = isset($_POST["checkboxname"]) ? " checked" : '' ;
echo "<input type='checkbox' name='checkboxname' value='yes'" . $checked . ">";
?>
This will retain the checkbox state after the form has been posted.
UPDATE:
For your code, just do it like this, I think:
$result = mysql_query("SELECT * FROM course") or die(mysql_error());
if ($result) {
while ($row = mysql_fetch_array($result)) {
$checked = '';
/* ERROR: if (isset($_POST['courses']) and $_POST['courses'] == $row['cid']) { */
if (isset($_POST['courses']) {
if (in_array($row['cid'], $_POST['courses']) {
echo $row['cid'];
$checked = " checked";
}
}
echo "<input type=\"checkbox\" name=\"courses[]\" value=\"$row[cid]\"" . $checked . ">$row['cname']\n";
}
}
EDIT: The condition needs to change too, I think, as I show in the code above.
http://localhost/?area=characters&name=Michal+Stroganof
$result = mysql_query("SELECT * from players WHERE name = '$_GET[name]'");
while ($row = mysql_fetch_assoc($result)) {
echo "Name: " .$row['name']. "<br>";
echo "Level: " .$row['level']. "<br>";
}
This is all code of my characters.php
If the get variable "name" is not included in the URL i want to show a search form that searches the table players. How would I do this?
Do you mean just to change your SQL string like so?
$sql = 'SELECT * from players';
if (isset($_GET['name'])) {
$safename = mysql_real_escape_string($_GET['name']);
$sql .= " WHERE name='$safename'";
}
$result = mysql_query($sql);
Be sure to sanitize your SQL!
Use isset():
if (isset($_GET['name'])) {
// your above code
} else {
// display form
}
Quick and dirty:
<?php
if (!isset($_GET['name']))
{
echo '<form action="'. $_SERVER['PHP_SELF'] .'" method="GET">'
.'<input type="text" name="name" />'
.'</form>';
}
else
{
// your current code that queries your database here
}
?>