Multiple checkbox management - html

Hello I generate my checkboxes using this way.
$result = mysql_query("SELECT * FROM tbl_tourism_type order by type_name ");
$i=1;
while($row = mysql_fetch_array($result)){
echo '<input type="checkbox" name="type[]" value="'.$row['type_id'].'" id="'.$row['type_name'].'">'.'
<label for="'.$row['type_name'].'" class="fil_lab">'.$row['type_name']. '</label>';
if($i%5==0)
{
$i = 0;
echo '<br><br>';
}
$i++;}
Note that they will most likely generate 50+ checkboxes. Currently they are generated by 5 each then next line and the way they are set up they are inconsistent with one another and are hard on the eyes. Would anyone know how to fix this so that they would be properly structured?Link to screen shot

Use CSS to give the checkbox a width and the label a width. That way they tale up the same amount of space.
echo '<input type="checkbox" name="type[]" value="'.$row['type_id'].'" id="'.$row['type_name'].'" style="width: 20px;" />'.'
<label for="'.$row['type_name'].'" class="fil_lab" style="width: 100px;">'.$row['type_name']. '</label>';

Related

How to write the query to accept the input from the search box and search from MySql db (Wordpress)

I'm trying to access data from local wamp server from a Wordpress site using a Search box. I created the search box using the function get_search_form(), and I am unable to write a query in php to access using the same.
I have used Wamp server (localhost) and a Wordpress site.
I have tried writing an html code for the search box and tried to access the data using it. But it didn't work. I felt it's easy to run a single php script rather than a separate html and php scripts.
Code to fetch data from db:
$connect = mysqli_connect("localhost", "root", "", "mydb");
$output = '';
if(isset($_POST["query"]))
{
$search = mysqli_real_escape_string($connect, $_POST["query"]);
$query = "
SELECT * FROM clients;
WHERE Name LIKE '%".$search."%'
OR Aadhar LIKE '%".$search."%'
OR Mobile LIKE '%".$search."%'
OR Company LIKE '%".$search."%'
OR Description LIKE '%".$search."%'
";
}
else
{
$query = "SELECT * FROM clients ORDER BY Name";
}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>Name</th>
<th>Aadhar</th>
<th>Mobile</th>
<th>Company</th>
<th>Description</th>
</tr>
';
while($row = mysqli_fetch_array($result)
{
$output .= '
<tr>
<td>'.$row["Name"].'</td>
<td>'.$row["Aadhar"].'</td>
<td>'.$row["Mobile"].'</td>
<td>'.$row["Company"].'</td>
<td>'.$row["Description"].'</td>
</tr>
';
}
echo $output;
}
else
{
echo 'Data Not Found';
}
I am successfully able to access all the data using this code.
First of all the function get_search_form(); will create a Search Box and a Submit button with a wrapper form. Form method is GET so $_POST in your code is completely wrong. Next is the search box created using this function have the name "s". The below code will be generated through the function :
<form role="search" method="get" class="search-form" action="">
<label>
<span class="screen-reader-text">Search for:</span>
<input type="search" class="search-field" placeholder="Search …" value="" name="s">
</label>
<input type="submit" class="search-submit" value="Search">
</form>
So change your code $_POST['query'] with $_GET['s']. Hope it will work for you.

HTML Form data from checkboxes within while loop

I have a really basic Date system on a site which displays the dates from the Database using a While loop, next to each date that is listed is a check box.
I would like the user to be able to select as many dates as they would like and click submit to remove these dates or edit these dates.
I am having trouble figuring out how I would get the data from multiple check-boxes using only one within the while loop which could be used multiple times?
Here is my current code:
<form action="" method="post">
<b>Current Dates:</b> <button type="submit" id="remove_dates" name="remove_dates" class="btn btn-danger btn-sm">Remove Selected</button>
<hr>
<div style="overflow-y: scroll;height:100px">
<?
$sqldate = "SELECT * FROM `foiu51r_calendar` ORDER BY date DESC";
$resultdate = $conn->query($sqldate);
if ($resultdate->num_rows > 0) {
// output data of each row
while($rowdate = $resultdate->fetch_assoc()) {
$date_id = $rowdate['date_id'];
$dates = $rowdate['date'];
?>
<input type="checkbox" id="date_id" name="date_id" value="<? echo $date_id; ?>"> <b><? echo date("d/m/Y", strtotime($dates)); ?></b><br>
<?
}
}
?>
</div>
</form>
if(isset($_POST['remove_dates'])){
$date_id = $_POST['date_id'];
}
I believe if you add this: [], to the name attribute, like this: <input type="checkbox" id="date_id" name="date_id[]" value="<? echo $date_id; ?>">. Then it will be handled as an array, which you can loop through with a foreach loop. and insert it into a database (or whatever you like).
Declare the name as an array, like as follows:
<input type="checkbox" id="date_id" name="date_id[]" value="<? echo $date_id; ?>">.
On submitting it will be handled as an array,
Then, you can loop the values of an array and for each element of an array use the id to remove the dates.

Getting PHP and SQL scripting to work properly

So, from what I have been learning for these past few weeks I believe I have sufficient knowledge on how to perform PHP, and SQL related queries to create a good and dynamic website that could support something like a forum. I've not been able to do that yet, and am having quite a bit of trouble with it as well. So far, I've made a PHP file, that was simply to see if I could use PHP well. It did not work out, and I've been getting plenty of errors, and I've been unable to fix them, whatsoever. And so, I'd like to come here to ask, if anyone out there could possibly analyze my code that I've written, and see what is wrong with it, if possible. Along with that, I'd like to know what would be the "Proper" way of
A. Connecting to SQL
B. Selecting Data
C. Displaying/Utilizing Data
And thank you, for reading and/or possibly replying to this.
Here, is the code I've written but have been unable to work.
<?php
include 'header.php';
include 'connect.php';
?>
<body>
<form>
Input First name:<br>
<input type="text" name="FN">
<br>
Input Last name:<br>
<input type="text" name="LN">
<br>
Input Email:<br>
<input type="text" name="Email">
<br>
<input type="submit" method="post">
<?php
if (isset($_POST['FN'], $_POST['LN'], $_POST['Email']))
$sql = 'INSERT INTO `info` ("USERID", "FN", "LN", "Email") VALUES (\'$_POST[FN]\', '$_POST["LN"]', '$_POST["Email"]')';
?>
</form>
<?php
$sql = "SELECT FN, LN, Email
FROM
info"
$result = "mysql_query($sql)"
while($row_list = mysql_fetch_assoc( $result )) {
ECHO <div>The Names are:</div><br>
ECHO $FN . "," . $LN . "," . $Email;
}
?>
</body>
</html>
Your PHP code is wrong in so many ways even in your query. What I did is clean your codes.
<?php
include 'header.php';
include 'connect.php';
?>
<body>
<form action="" method="POST">
Input First name:<br>
<input type="text" name="FN">
<br>
Input Last name:<br>
<input type="text" name="LN">
<br>
Input Email:<br>
<input type="text" name="Email">
<br>
<input type="submit" name="submit-btn" value="submit">
</form>
<?php
if (isset($_POST['submit-btn'])){
$sql = 'INSERT INTO info ( "FN", "LN", "Email") VALUES ('$_POST[FN]', '$_POST["LN"]', '$_POST["Email"]')';
if (mysql_query($sql)) {
echo "New record created successfully";
}
}
$sql = "SELECT FN, LN, Email FROM info";
$result = mysql_query($sql)
while($row_list = mysql_fetch_assoc( $result )) {
ECHO '<div>The Names are:</div><br>';
ECHO $FN . "," . $LN . "," . $Email;
}
?>
</body>
</html>
try to indent your code to make it more readable for yourself.
as already answered by user3814670, your insert query was wrong, with 4 elements (id,fn,ln,email) and only 3 data (fn,ln,email)
your query was't being executed also cleaned by user3814670 by adding the lines
if (mysql_query($sql)) {
echo "New record created successfully";
}
try to print your query to the screen and executing it in you database to see if your query fails or print the error to screen
mysql_error()
add this on top of your file after
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Here's how you display data from the database using while loop
while($row=mysql_fetch_array($result)) {
echo $row['FN'] . " " . $row['LN'] . " " . $row['Email'];
}

How to center allign input forms

In my page I am having one drop-down, 3 text boxes and one submit button .As of now it is left alligned . I need to make it center alligned. I am using bootstrap css in my code. I am new to this field . Can someone tell how to do that . I have tried this:
<?php
echo "<form action=page2.php method=GET>";
echo '<td><select class="form-control" name="select-val">
<option value="a1">A1</option>
<option value="a2">A2</option>
<option value="a3">A3</option>
<option value="a4">A4</option>
<option value="a5">A5</option>
<option value="a6">A6</option>
</select></td>';
echo "<br>";
echo "<td>" ." <input align='center' type = text class = form-control name = sel_val
placeholder = 'Enter The Name'>". "</td>";
echo"<br>";
echo"<br>";echo "<td>" ." <input align='center' type = text class = form-control name = start_date
placeholder = 'Start Date(YYYY-MM-DD)'>". "</td>";
echo"<br>";
echo "<td>" ." <input align='center' type = text class = form-control name = end_date
placeholder = 'End Date(YYYY-MM-DD)'>". "</td>";
echo"<br>";
echo "<td>" ."<input align='center' class=btn type=submit value=select". "></td>";
echo "</form>";
?>
Thanks in advance.
First of all, don't use tables for layouts; it's bad practise if you aren't showing any tabular data.
There are a couple of ways of going about this
Method 1) I prefer this one as it makes the most sense but if you want the elements to be in different rows you can't use this method
On all the inputs and elements you want centered add display: inline-block. Then on the container element that encapsulates all of this add text-align: center.
This will center all the elements horizontally and should appear how you want it.
Method 2) You can also add a position: relative to all the elements and then do left: 50%; margin-left: -[half of your element width]. This also works and allows the elements to be on different rows while remaining horizontally centered.

Inserting multiple row in mysql with codeigniter

I am trying to upload multiple images with description in codeigniter. Everything is fine, image is uploaded, image filename is sent to database however "description" is not going to database, where did i mistake ?
form code is like this
File 1: <input type="file" name="image1" /><br />
Descr: <input type="text" name="description[]" size="50" value="" /><br/><br/>
File 2: <input type="file" name="image2" /><br />
Descr: <input type="text" name="description[]" size="50" value="" /><br/><br/>
File 3:<input type="file" name="image3" /><br />
Descr: <input type="text" name="description[]" size="50" value="" /><br/><br/>
and model is this
for($i = 1; $i < 6; $i++) {
/* Handle the file upload */
$upload = $this->upload->do_upload('image'.$i);
/* File failed to upload - continue */
if($upload === FALSE) continue;
/* Get the data about the file */
$data = $this->upload->data();
$uploadedFiles[$i] = $data;
/* If the file is an image - create a thumbnail */
if($data['is_image'] == 1) {
$configThumb['source_image'] = $data['full_path'];
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
}
$image['raw'] = $data['raw_name'];
$image['ext'] = $data['file_ext'];
$image['newsid'] = $_POST['newsid'];
$image['description'] = $_POST['description'][$i];
$this->db->insert('multipics', $image);
}
Please note that the above code makes multiple rows insertion, I just need the description field to go in database
As a work around, I would get the POST variable first outside of the loop:
// (1) Get the POST description variable outside of the look
// As a precaution, dump or echo $description to make sure it looks right...
$description_ = $this->input->post('description');
for ($i = 1; $i < 6; $i++)
{
...
// Put some checks to make sure $i is an index...
$image['description'] = $description[$i];
...
}
Try this and let us know how you get on!
Change Change name="description[]" to name="description
You may also want to look at CI's insert_batch() method. This will let you insert all of the records with one query.