PHP:
function get_t_wrinkle_rel(){
global $mysqli;
$q = $mysqli->query("SELECT * FROM t_wrinkle_rel ORDER BY t_wrinkle_name ASC");
while ($r = $q->fetch_array()) :
echo '<p><input type="checkbox"' . $r['t_wrinkle_name'] . '</p>';
endwhile;
}
RESULT:
<input type="checkbox" value="Crows feet">Crows feet
<input type="checkbox" value="Frown lines" >Frown lines
<input type="checkbox" value="Lip augmentation">Lip augmentation
<input type="checkbox" value="Lip lines">Lip lines
<input type="checkbox" value="Marionette lines">Marionette lines
i want the result:
**LEFT** **RIGHT**
<input type="checkbox">Crows feet |<input type="checkbox" >Lip lines
<input type="checkbox">Frown lines | <input type="checkbox">Marionette lines
<input type="checkbox"">Lip augmentation
Could you not alternate a class on each second input that suggests clearing a float or something similar if you are not floating the inputs? Something along the lines of:
function get_t_wrinkle_rel(){
global $mysqli;
$flag = 1;
$q = $mysqli->query("SELECT * FROM t_wrinkle_rel ORDER BY t_wrinkle_name ASC");
while ($r = $q->fetch_array()) :
if ($flag = 1) {
$orientate=left;$flag=0;
} else {
$orientate=right;$flag=1;
}
echo '<p><input class="' . $orientate . '"type="checkbox"' . $r['t_wrinkle_name'] . '</p>';
endwhile;
}
while ($left = $q->fetch_array()) {
echo '<p>';
echo '<input...>' . $left['...'];
if ($right = $q->fetch_array()) {
echo '| <input...>' . $right['...'];
}
echo '</p>';
}
This way should fill the left column in order then the right column, keeping the order you want to achieve. I don't have access to your database so I can't test this, but it should do the job.
function get_t_wrinkle_rel(){
global $mysqli;
$q = $mysqli->query("SELECT * FROM t_wrinkle_rel ORDER BY t_wrinkle_name ASC");
$mid = floor($q->num_rows/2); // Get halfway point
$count = 0;
$array = array();
while($r = $q->fetch_array()){
$string = '<input type="checkbox" value="'.$r['t_wrinkle_name'].'" />'.$r['t_wrinkle_name'];
if($count <= $mid){
// Left column
$array[$count] = $string;
} else {
// Right column
$array[$count-$mid] .= '|'.$string;
}
$count++;
}
// Make single string
echo implode('', $array);
}
However I would recommend using the idea biscuitstack suggested, using CSS to position it the way you want, rather than doing it programatically. It's always better to try to keep presentation separate from the logic wherever possible.
You could use flag that increments everytime and
<input type="checkbox"' . $r['t_wrinkle_name'] .
if flag % 2 == 0 then echo '<br />'
Or something like that.
Related
I'm using HTML, PHP and Ajax to toggle switch in my website. However, only the top most switch is functional. The reason why I use PHP is to display results from my DB, and Ajax is there to toggle switch without reloading the website. Thanks in advance and comment for any questions!
Photo Here :D I have three rows in the DB. Data retrieve fine. Top button works!
ps : removed all classes for simplicity
About main.php and recipe.inc.php. They are separated because recipe.inc.php is used in many documents.
main.php
<?php
$conn = mysqli_connect(localhost,****,****,loginsystem);
//DB connection
$query="SELECT * FROM `recipe` WHERE creator='$uid'";
//SQL Query
$results = mysqli_query($conn,$query);
$array = array();
//Array to save key column of the result in order
while ($row = mysqli_fetch_assoc($results)) {
for ($i = 0; $i < count($row[recipe_ID]); $i++) {
$array[$i] = $row[recipe_ID];
echo '<input type="hidden" id="recipe_ID" name="recipe_ID" value="';
echo $array[$i];
echo '">';
//might confuse you. this is just to hand over recipe_ID to recipe.inc.php
echo '<li>';
echo '<label>';
if($row[status]==1){
echo '<input type="checkbox" id="checkStatus" name="checkStatus" checked="">';
//In case where row[status] is equal to 1. means it's ON
}else{
echo '<input type="checkbox" id="checkStatus" name="checkStatus">';
//OFF otherwise
}
echo '<span class="switcher-indicator"></span>';
echo '</label>';
echo '</li>';
}
}
?>
recipe.inc.php
<?php
if(isset($_POST['checkStatus'])){
$recipe_ID = $_POST['recipe_ID']);
if($_POST['checkStatus']=='ON'){
$status = 1; //to save in DB as boolean. if ON->1
}else if($_POST['checkStatus']=='OFF'){
$status = 0; //if OFF->0
}
$sql = "UPDATE `recipe` SET `status`=$status WHERE creator=$uid AND recipe_ID=$recipe_ID";
//nev
mysqli_query($conn,$sql);
}
?>
and the Ajax part is saved in another js file.
recipe.js
$(document).ready(function() {
$('#checkStatus').on('click', function() {
var checkStatus = this.checked ? 'ON' : 'OFF';
var recipe_ID = $("#recipe_ID").val();
$.post("loginsystem/includes/recipe.inc.php", {
"checkStatus": checkStatus,
"recipe_ID": recipe_ID
},
function(data) {
$('#checkStatus').html(data);
});
});
});
That sounds like all the switches have the same ID. If that is the case, only the first one will work.
Try changing your code to look like this:
<?php
$conn = mysqli_connect(localhost,****,****,loginsystem);
//DB connection
$query="SELECT * FROM `recipe` WHERE creator='$uid'";
//SQL Query
$results = mysqli_query($conn,$query);
$cnt = 1;
$array = array();
//Array to save key column of the result in order
while ($row = mysqli_fetch_assoc($results)) {
for ($i = 0; $i < count($row[recipe_ID]); $i++) {
$array[$i] = $row[recipe_ID];
echo '<input type="hidden" id="recipe_ID-' .$cnt. '" name="recipe_ID" value="'; <============== HERE
echo $array[$i];
echo '">';
//might confuse you. this is just to hand over recipe_ID to recipe.inc.php
echo '<li>';
echo '<label>';
if($row[status]==1){
echo '<input type="checkbox" id="checkStatus-' .$cnt. '" name="checkStatus" checked="">'; //<============== HERE
//In case where row[status] is equal to 1. means it's ON
}else{
echo '<input type="checkbox" id="checkStatus-' .$cnt. '" name="checkStatus">'; //<================ HERE
//OFF otherwise
}
echo '<span class="switcher-indicator"></span>';
echo '</label>';
echo '</li>';
}
$cnt++; <=========== HERE
}
?>
I'm trying to create my first function in PHP, a drop down. It's almost working, but when I submit/post the data the options with a space in it will only show the first word in the output.
In the example I have 'First Name' as an option in the drop down list. When I hit submit, the output will only be 'First'. When I change 'First Name' to 'First-Name' it works, the output is 'First-Name'. So I think I need to add quotes somewhere in the code so that it is handled as a string??
Hope someone can help me out, I'm so close to what I want.
<!DOCTYPE HTML>
<html>
<body>
<?php
include('db_functions.php');
function clean_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fName = clean_input($_POST['first_name']);
}
//drop down function
function dropdown($name, $options) {
$dropdown = '<select type="text" name='.$name.'>'."\n";
$rows = $options;
if($rows === false) {
$error = db_error();
}
$totalrows = count($rows);
for ($x = 0; $x < $totalrows; $x++) {
$dropdown .= '<option value=' . $rows[$x][$name] . '>' . $rows[$x][$name] . '</option>'."\n";
}
$dropdown .='</select>'."\n";
return $dropdown;
}
echo '<form action="' . htmlspecialchars($_SERVER["PHP_SELF"]) . '" method="post">';
//start dropdown
echo 'First Name:';
$name = 'first_name';
$options = db_select("SELECT DISTINCT first_name FROM nametable GROUP BY first_name ORDER BY first_name ASC LIMIT 20");
echo dropdown($name, $options);
//end dropdown
echo '<input type="submit" name="submit" value="Submit">';
echo '</form>';
echo $fName;
?>
</body>
</html>
Your code is writing
<option value=First Name>
The space ends the value, so Name is a separate attribute. You need to put the value in quotes, so it's
<option value="First Name">
The code should be:
$dropdown .= '<option value="' . $rows[$x][$name] . '">' . $rows[$x][$name] . '</option>'."\n";
I have a function that creates a radio button in PHP:
// This function creates a radio button.
// The function takes two arguments: the value and the name.
// The function also makes the button "sticky".
function create_radio($value, $name = 'gallon_price')
{
// Start the element:
echo '<input type="radio" name="' .
$name .'" value="' . $value . '"';
// Check for stickiness:
if (isset($_POST[$name]) && ($_POST[$name] == $value))
{
echo ' checked="checked"';
}
// Complete the element:
echo " /> $value ";
} // End of create_radio() function.
I then leave the PHP form to create an html form and call the function three times with values that represent three different gas prices.
<span class="input">
<?php
create_radio('3.00');
create_radio('3.50');
create_radio('4.00');
?>
</span>
I am wondering how I could change this code so it would be possible to get the same output and only make one call to the create_radio function.
Thanks!
function create_radio($value, $name = 'gallon_price')
{
$output = "";
if (is_array($value)) {
while (count($value) > 0) {
$arr_value = array_pop($value);
$output .= create_radio($arr_value);
}
} else {
// Start the element:
$output .= '<input type="radio" name="' .
$name .'" value="' . $value . '"';
// Check for stickiness:
if (isset($_POST[$name]) && ($_POST[$name] == $value))
{
$output .= ' checked="checked"';
}
// Complete the element:
$output .= " /> $value ";
}
return $output;
}
A quick bit of recursion will allow the function to work for arrays and non arrays. Note: in the html you will need to echo the call to create_radio not just call it.
you could make $value an array create_radio(array('3.00','3.50','4.00')); just loop inside the function:
function create_radio($value,$name = 'gallon_price'){
foreach($value as $v){
// Start the element:
$out. = '<input type="radio" name="'.$name.'" value="'.$v.'"';
// Check for stickiness:
if(isset($_POST[$name])&&($_POST[$name]==$v)){
$out .= ' checked="checked"';
}
// Complete the element:
$out .= " /> $v ";
}
return $out;
} // End of create_radio() function.
call it:
echo create_radio(array('3.00','3.50','4.00'));
it is usually better not to echo inside the function.
I have an array with the names of medication which the user is currently taking. On the webpage, I want to display radio buttons with the name of each of the medications, which are currently being taken, next to them. The code I have for the array is;
//Current Entries Section
$CurrentMedsQuery = "SELECT Name FROM `".$username."medication` WHERE Status='Current';";
$RunMedsQuery = mysql_query($CurrentMedsQuery) or trigger_error(mysql_error().$CurrentMedsQuery);
$Count = mysql_num_rows($RunMedsQuery);
$CurrentMedsArray = array();
while ($CurrentMedEntries = mysql_fetch_array($RunMedsQuery)) {
array_push($CurrentMedsArray,$CurrentMedEntries['Name']);
}
$session =& JFactory::getSession();
$session->set('CurrentMedsArray', $CurrentMedsArray);
while ($Count < 0) {
}
$FirstMedEntry = ($CurrentMedsArray["0"]);
Then on my HTML form, I have the following code which successfully displays the name of the first element in the array as I want it to;
<form method="post" name="currentmeds" action="">
<input type="radio" name="med1" value="med1"><?php echo $FirstMedEntry;?><br>
</form>
But my question is, I will not know how many current medication entries the user has, therefore I cannot continuously use echo $FirstMedEntry, echo $SecondMedEntry and so on..I figure I need some sort of loop, and help would be greatly appreciated!
Thanks in advance!
Changed to the following as suggested;
//Current Entries Section
echo '<form method="post" name="currentmeds" action="">';
$CurrentMedsQuery = "SELECT Name FROM `".$username."medication` WHERE Status='Current';";
$RunMedsQuery = mysql_query($CurrentMedsQuery) or trigger_error(mysql_error().$CurrentMedsQuery);
$Count = mysql_num_rows($RunMedsQuery);
$count = 1;
while ($CurrentMedEntries = mysql_fetch_row($RunMedsQuery)) {
echo '<input type="radio" name="med' . $count . '" value="med' . $count . '">' . $CurrentMedEntries . '<br>';
$count++;
}
echo '</form>';
But now receiving this error;
Notice: Array to string conversion in C:\xampp\htdocs\Joomla-Lifestyle\components\com_jumi\files\medication.php on line 238
Line 238:
echo '<input type="radio" name="med' . $count . '" value="med' . $count . '">' . $CurrentMedEntries . '<br>';
$CurrentMedsArray contains all your data, so you can just loop through it and display the values:
<form method="post" name="currentmeds" action="">
<?php foreach ($CurrentMedsArray as $key => $entry): ?>
<input type="radio" name="med<?=$key+1?>" value="med<?=$key+1?>"><?=$entry;?><br>
<?php endforeach ?>
</form>
If the array isn't needed for this purpose, this could be modified and displayed inside your while loop instead. Also note <?=$var;?> is the short syntax for <?php echo $var; ?>.
You don't need to use an array and I'd suggest you don't. Just do the following:
<?php
echo '<form method="post" name="currentmeds" action="">';
$CurrentMedsQuery = "SELECT Name FROM `".$username."medication` WHERE Status='Current'";
$RunMedsQuery = mysql_query($CurrentMedsQuery) or trigger_error(mysql_error().$CurrentMedsQuery);
$Count = mysql_num_rows($RunMedsQuery);
$count = 1;
while ($CurrentMedEntries = mysql_fetch_row($RunMedsQuery)) {
echo '<input type="radio" name="med' . $count . '" value="med' . $count . '">' . $CurrentMedEntries['name'] . '<br>';
$count++;
}
echo '</form>';
?>
Although I strongly urge you to not use mysql_. Instead, use mysqli_ or PDO.
I try to create an order page on php. I have created a form which stores data into the database. The form number is based on the user and it is created dynamic.
<?php
if(isset($_POST['submit_num']))
{
$number=$_POST['sky'];
for($i=0;$i<$number;$i++)
{
$item = $_SESSION['item'];
echo $item;
$rec_query = "SELECT * FROM ylika";
$rec_result= mysql_query($rec_query) or die("my eroors");
echo '<form action="user_order_form.php" method="POST">';
echo '<html>';
while($row_rec = mysql_fetch_array($rec_result))
{
echo '<input type="checkbox" name="yliko[]" value='.$row_rec['onoma'].'> '.$row_rec['onoma'].'';
echo '<br>';
}
echo '<br>';
echo '<input type="submit" name="submit" value="ORDER">';
echo '</form>';
}
}
?>
So I have many forms and 1 script to handle them. If I submit 1 form then it applies the data to the database but the other forms dissapear. This is the second php 'handler'.
<?php
if (isset($_POST['submit']))
{
$max_id = "SELECT MAX(id_order) FROM id_of_orders";
$x=mysql_query($max_id) or die("my eroors");
$id= mysql_fetch_array($x);
$xyz = $id['MAX(id_order)'];
$item = $_SESSION['item'];
$temp = $_POST['yliko'];
$temp2 = implode(",", $temp);
$inserts = ("INSERT INTO orders (order_id,product,ulika) VALUES ('$xyz' , '$item','$temp2')");
$inc_prod=("UPDATE proion SET Counter = Counter + 1 WHERE proion.onomasia='$item'");
mysql_query($inserts) or die(mysql_error());
mysql_query($inc_prod) or die(mysql_error());
}
?>
I want to submit all the forms. Should I try to create one submit button for all of the forms or is there a way to handle all of them separately ?
You can have many submit forms, but simply define one big <form> with all of them, then you will not lose any information.
<?php
if(isset($_POST['submit_num']))
{
$number=$_POST['sky'];
echo '<form action="user_order_form.php" method="POST">';
for($i=0;$i<$number;$i++)
{
$item = $_SESSION['item'];
echo $item;
$rec_query = "SELECT * FROM ylika";
$rec_result= mysql_query($rec_query) or die("my eroors");
echo '<html>';
while($row_rec = mysql_fetch_array($rec_result))
{
echo '<input type="checkbox" name="yliko[]" value='.$row_rec['onoma'].'> '.$row_rec['onoma'].'';
echo '<br>';
}
echo '<br>';
echo '<input type="submit" name="submit" value="ORDER">';
}
echo '</form>';
}
?>
now you can also add the ~$i~ identifier to input names, so you can easily distinguish which is which.