Please see database's image also
I want to show my checkbox values from database.
Example : my value is 25,26.
So, How can i display it one by one ?
such as,
-- 1st value is : 25.
--2nd Value is : 26.
my code:
$db = new mysqli("localhost","paroshic_paroshic","kxmcwQzLTrTR","paroshic_matri2018jl");
$sql = "select * from tbldatingusermaster order by userid desc";
$result = $db->query($sql);
while($data = $result->fetch_object()){
echo $data->education; //My value is 25,26
}
Thanks.
You can write html tags to page with echo, you know that. So basicly it will be something like this:
while($data = $result->fetch_object()){
echo '<input type="checkbox" name="check" value="'.$data->educationid.'">'.$data->educationid.'<br>';
}
I think the piece of the puzzle that you were looking for is perhaps explode to create an array of integers from the given string $data->educationid (ie: 25,26 )
$db = new mysqli("localhost","paroshic_paroshic","kxmcwQzLTrTR","paroshic_matri2018jl");
$sql = "select * from tbldatingusermaster order by userid desc";
$result = $db->query( $sql );
if( $result ){
while( $data = $result->fetch_object() ){
/* explode the string into little integers */
$ids=explode( ',', $data->educationid );
/* iterate through the pieces and generate an input[checkbox] element */
foreach( $ids as $id )printf('<input type="checkbox" name="UNKNOWN[]" value="%s" />',$id);
}
}
Related
Good day! I am trying to get the user ids based on the names submitted on the input field. But the loop only returns the user id of the first selected user. Can somebody please help me?
Here's the code:
if(isset($_POST['proponent'])){
$proponent = mysqli_real_escape_string($con,$_POST['proponent']);
$myArray = explode(',', $proponent);
$posts = array();
foreach($myArray as $item) {
$query = "SELECT user_id FROM user WHERE CONCAT(fname, ' ', lname) = '$item'";
$get = mysqli_query($con, $query);
array_push($posts, mysqli_fetch_assoc($get));
print_r($posts);
}
}
Using mysqli_fetch_assoc only once will only get you the first row if it exists. So you will need to add this in a loop, say while loop like below in order for the mysqli_function to move it's pointer over the entire result set.
while($row = mysqli_fetch_assoc($get)){
$posts[] = $row;
}
This seems like it should be really straightforward, but I keep getting unexpected output. I'm trying to access specified rows in a SQL database which each contain a numerical value and then calculate the sum of those values. PHP is concatenating the values as if they were strings even after I've set the datatype of the values to float. My code:
$query = "SELECT * FROM populations WHERE username ='{$_SESSION[name]}' AND region_name = 'region'"
$query .= "AND city_name = 'city'";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
$population_value = $row['population_value'];
$population_value = is_array($population_value) ? $population_value : array($population_value);
foreach($population_value as $value){
echo $value;
}
echo array_sum($population_value);
}
I have also tried:
$total = array("");
foreach($population_value as $value){
floatval($value);
array_push($total, $value);
echo $value;
}
echo array_sum($total);
My output is always something like: 100002000030000
with 10,000 20,000 and 30,000 being the values of each population.
I've successfully calculated sums using foreach with values that weren't retrieved from MySQL.
What is going on here?
$query = "SELECT * FROM populations WHERE username ='{$_SESSION[name]}' AND region_name = 'region'"
$query .= "AND city_name = 'city'";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
$population_value = $row['population_value'];
//This is actually rewriting the array or NOT adding the value to it.
$population_value = is_array($population_value) ? $population_value : array($population_value);
//ok, so you're going to repeatedly output this?
foreach($population_value as $value){
echo $value;
}
echo array_sum($population_value);
}
I think what you want is this:
$query = "SELECT * FROM populations WHERE username ='{$_SESSION[name]}' AND region_name = 'region'"
$query .= "AND city_name = 'city'";
$result = mysqli_query($connection, $query);
$population_value=array(); //Initialize the array so we can just add to it.
while($row = mysqli_fetch_assoc($result)) {
$population_value[]= intval($row['population_value']); //Just making sure we get a number.
echo end($population_value); //We could output the row again, or just grab the last element of the array we added.
}
//Now that the array is fully populated and we've made sure it's only numbers, we output the grand total.
echo array_sum($population_value);
First, don't initialize the array with an empty string. Do this instead:
$total = array();
or with the new style:
$total = [ ];
Second, rewrite the floatval thing like this:
array_push($total, floatval($value));
That should fix it...
I have a PHP function that extract dat of an invoice from DB.
The invoice may have more then one line (one product).
function getInvoiceLines($id)
{
$res = mysql_query("select * from invoice_lines where id = $id ");
while ($row = mysql_fetch_array($res))
{
$res_retun['ref']=$row['ref'];
$res_retun['label']=$row['label'];
$res_retun['price']=$row['label'];
$res_retun['qty']=$row['qty'];
}
return $res_retun ;
}
I found this link Create a Multidimensional Array with PHP and MySQL and I made this code using that concept.
Now, how can I move something like a cursor to the next line and add more lines if there's more in MySQL result??
If it's possible, how can I do to show data in HTML with for ??
A little modification should get what you want, below the [] operator is a shorthand notation to add elements to an array, the problem with your code is that you are overwriting the same keys on each iteration
// fetch only what you need;
$res = mysql_query("select ref, label, price, qty from invoice_lines where id = $id ");
while ($row = mysql_fetch_array($res))
{
$res_return[] = $row;
}
return $res_return;
Note, I fixed some of your typos (you were using $rw instead of $row in the loop of your original code)
If you used PDO with fetchAll() you would be returned with the array your expecting, also be safe from nastys:
<?php //Cut down
$db = new PDO("mysql:host=localhost;dbname=dbName", 'root', 'pass');
$sql = "SELECT * FROM invoice_lines WHERE id = :id ";
$stmt = $db->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->execute();
$res_return = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
Then you just loop through the array like with any other array:
<?php
foreach($res_return as $row){
echo $row['ref'];
...
...
}
?>
Also id should not have more then 1 row it should be unique IF its your primary key.
If you were using PDO like you should be it would be super easy and you would solve your sql injection issues
$db = new PDO(...);
function getInvoiceLines( $db, $id )
{
$stmnt = $db->prepare("select ref, label, price, qty from invoice_lines where id=?");
$stmnt->execute( array( $id ) );
return $stmnt->fetchAll( PDO::FETCH_ASSOC );
}
Make variable global first Then access it in function. For exp.
$return_arr = array(); // outside function
$i = 0;
cal_recursive(); // call function first time
function cal_recursive(){
global $return_arr;
global $i;
$return_arr[$i] = // here push value to array variable
$i++;
// do code for recursive function
return $return_arr // after end
}
I am stuck in writing a PHP function. What I need to do is pass on about 10 - 12 ID into an array and then sort them by name in mysql database and then print some statements. I got this far where I manually add an ID number and it prints the information I need but at this point I have to sort the data before using those ID numbers.
Here is what I have done so far.
<?php
function print_software_info($id) {
$appinfo = "SELECT * FROM `appinfo` WHERE `id` = ".intval($id);
$rt = mysql_query($appinfo) or die(mysql_error("Could not retrieve database information"));
echo $rt['name'];
echo $rt['image'];
}
?>
<?php
print_software_info(217);
print_software_info(179);
print_software_info(8);
?>
with
$ids = array(13, 14, 250, ... );
First (optional for now, but keep in mine) make sure they're all integer (sanitize)
$ids = array_map('intval',$ids);
You need to build a query like this
$q = "SELECT * FROM `appinfo` WHERE `id` IN (". implode(',',$ids) ." ) ORDER BY name";
$result = mysql_query($q);
Then go through each rows..
while ($row = mysql_fetch_object($result)) {
echo $row->name;
echo $row->image;
}
I have the following code and want to manually select an array:
<?php
$articleQuery = mysql_query("SELECT * FROM articles WHERE topic = 'IT' ");
while($article= mysql_fetch_array($articleQuery )){
$aid = $article['id'];
$media = $article['media'];
$link = $article['link'];
}
echo $aid[0];
?>
The problem is that its not really selecting/displaying the correct information. What I want is to be able to select the value of the first array.
Thanks in advance.
$firstrow = null;
while($article= mysql_fetch_array($articleQuery)) {
if ($firstrow === null)
$firstrow = $article;
$aid = $article['id'];
$media = $article['media'];
$link = $article['link'];
//manipulate $aid, $media and $link.
}
//manipulate $firstrow
If you only need the first row, limit the query to one result and execute mysql_fetch_array at most once, instead of in a loop.
Or you can do like this:
$array = array();
while($article = mysql_fetch_object($articleQuery )){
$array[] = $article;
}
echo $array[0]->id; // get first id
echo $array[0]->media; // get first media
echo $array[0]->link; // get first link
echo $array[1]->id; // get second id
echo $array[1]->media; // get second media
echo $array[1]->link; // get second link
// and so on.......
if you want $aid to be an array, you should do something like that:
$aid = array();
while($article= mysql_fetch_array($articleQuery )){
$aid[] = $article['id'];
}
The problem is that its not really
selecting/displaying the correct
information. What I want is to be able
to select the value of the first
array.
What you want is propably this:
$articleQuery = mysql_query("SELECT * FROM articles WHERE topic = 'IT' ");
$article= mysql_fetch_array($articleQuery);
echo $article[0];
You have unnecessary loop. You can also add "limit 1" to sql query. Although I'm not sure I understand your goal correctly.
Using mysql_fetch_assoc, will use the field names as the array indexer, so $article['id'] instead of $article[0]. That way if you change the definition of the table by adding new columns, your code won't break!
$articleQuery = mysql_query("SELECT * FROM articles WHERE topic = 'IT' LIMIT 1");
$article= mysql_fetch_assoc($articleQuery);
var_dump($article);
If you really only want the first result:
$articleQuery = mysql_query("SELECT * FROM articles WHERE topic = 'IT' LIMIT 1"); // note LIMIT clause
if( false !== ($article = mysql_fetch_array($articleQuery )))
{
$aid = $article['id'];
$media = $article['media'];
$link = $article['link'];
}
echo $aid;
If you want them all, but indexable:
$articleQuery = mysql_query("SELECT * FROM articles WHERE topic = 'IT' ");
while($article= mysql_fetch_array($articleQuery ))
{
$aid[] = $article['id'];
$media[] = $article['media'];
$link[] = $article['link'];
}
echo $aid[0];
Why not edit your SQL statement to select only one item?
mysql_query("SELECT * FROM articles WHERE topic = 'IT' LIMIT 1");
But the error in your code, is that you're looping over all your selected records, overwriting your variables on each pass. If you want to store all the rows as an array, you should modify your syntax like this:
while($article= mysql_fetch_array($articleQuery )){
$aid[] = $article['id'];
$media[] = $article['media'];
$link[] = $article['link'];
}
...after which you could access the first row with aid[0].
But instead I'd suggest a different structure:
while($article= mysql_fetch_array($articleQuery )){
$articles[]['aid'] = $article['id'];
$articles[]['media'] = $article['media'];
$articles[]['link'] = $article['link'];
}
What that does, is collect all the data into a single data structure, where each record holds all the data related to the article. You would access it like this:
echo $articles[0]['aid'];
echo $articles[0]['media'];
echo $articles[0]['link'];
If this looks like hebrew to you, take a look at the PHP manual section for arrays.