I have some id's from the ajax code posted into a php file.it is in the format of an array.
$members=$_REQUEST['members'];
$temp=explode(',',$members);
$count=0;
foreach($temp as $value) {
$count++;
}
echo $count;
print_r($temp);
upto this the code works.I want to insert the ids to the member_id field of my database table.I tried implode function,then it shows
'undefined' as error.
Related
Problem
I have a form table with multiple textbox and one submit button
Im using implode but all value's are inserted in array
$val = implode(',',$textboxval);
result is (80,80,80,40,80,90,70,70,70)
What i want is i want to insert each row of data's in an group of array in the database
[0]=(80,80,80),1=(40,80,90),2=(70,70,70)
RESULT
You could use array_chunk:
foreach (array_chunk($textboxval, 3) as $values) {
$val = implode(',', $values);
// insert $val into database
}
I am Using codeigniter to generate a ajax data-table and passing condition as User Id to fetch the records form a product orders table.
$this->datatables->select('orders.order_user_id,orders.order_date,orders.order_id,orders.order_confirmation_status,orders.order_payment_status,orders.order_shipping_status,orders.order_delivery_status,orders.order_packing_status')
->join('order_products','order_products.order_id=orders.order_id')
->select('round(sum(order_products.pr_quantity*order_products.pr_price))')
->where('orders.order_user_id',$this->uri->segment(3))
->unset_column('orders.order_confirmation_status')
->unset_column('orders.order_payment_status')
->unset_column('orders.order_shipping_status')
->unset_column('orders.order_delivery_status')
->unset_column('orders.order_packing_status')
->add_column('status', '$1','purchase_details_status(orders.order_confirmation_status,orders.order_packing_status,orders.order_shipping_status,orders.order_delivery_status)')
->add_column('action', '$1','purchase_details_action(orders.order_confirmation_status,orders.order_packing_status,orders.order_shipping_status,orders.order_delivery_status,orders.order_id)')
->from('orders');
echo $this->datatables->generate();
}
Here i Pass user_id in Where Condition. I have 10 records in database.but the table generate only one row.how to do this like foreach in codeigniter to get all 10 records.
you are echoing only the first element of the result,
so put your result in a variable and loop through it
$results = $this->datatables->generate();
foreach ($results as $row)
{
print_r($row);
}
I hope that helps
I want to update my database inside the foreach statement but it gives me the same values that I insert on to the last value that I input.
Let's say I have a looping textbox at the previous page.
$i = 0;
while($row_recordset = mysql_fetch_array($query_run))
{
echo "<tr>";
<td'><input type='text' name='atten_ave".$i."''></td>
echo "</tr>";
$i++;
}
Now this code will get each value of the textbox and should be update the database from the previous page.
foreach($_POST as $textbox => $values) {
$sessionbatch = getbatchno('BATCHNO');
$query_update = "UPDATE `grades`
SET
`ATTEN_SUM` = '$values'
WHERE
`BATCHNO` = '$sessionbatch'
";
if(mysql_query($query_update)){
echo 'SUCCESS';
} else{
die(mysql_error());
}
when I check my ATTEN_SUM COLUMN the values are the SAME base on the last input on the textbox.
First off, do not loop through every value in $_POST. Give your textboxes an idenitcal name that has [] appended to it, like name="atten_ave[]". This will create an array for them within $_POST, then you can loop through them all safely with:
foreach($_POST["atten_ave"] as $textbox => $values) { ....
Now whenever getbatchno('BATCHNO'); returns a value it has already returned within the life of that loop, it will overwrite the update it previously did with that similar value. So if it is returning the same value in every iteration, your query will just keep overwriting itself.
Quick PHP/MySQL question:
I'm trying to display the data from a MySQL database table as an HTML table, and for some reason my code doubling the output of each piece of data.
This is my code:
$rowarray = $statement->fetchall();
print "<tr>\n";
foreach ($rowarray as $row) {
foreach ($row as $col) {
print "\t<td>$col</td>\n";
}
print "</tr>\n";
}
My results look something similar to this:
User ID | User Name | First Name | Last Name
1 1 User Name User Name First Name First Name Last Name Last Name
Etc etc. You get the idea. Why this is happening? By the way, if I manually add the column information by referring to row[] subscripts 0-3, everything is displayed properly; it's only when I use the nested foreach statements that the data is duplicated.
You are getting both a numeric and a name-indexed value back from PDO fetchall. To get just the numeric index:
$rowarray = $statement->fetchall(PDO::FETCH_NUM);
ok i have this and it doesn't show all the rows when fetched from mysql database its like this:
mysql_select_db($database_config, $config);
$query_cat = "SELECT * FROM cat";
$cat = mysql_query($query_cat, $config) or die(mysql_error());
$row_cat = mysql_fetch_array($cat);
$arr = array("TIT" => $row_cat['title'],
"DES" => $row_cat['description']);
foreach($arr as $ar){
echo $ar;
}
now it only displays the first row and then stops why is it not displaying all the fields and i don't wanna use while loop for it can anyone explain me the problem??
EDIT: Well basically i want to work it like this
$p = "{TIT}{DES}";
foreach($arr as $k => $p){
$p = str_replace("{".$k."}", $v, $p);
echo $p;
}
Now the problem is not with str_replace its with the loop or database because the database rows are not incrementing and displays only one data.
This will always return the first row. you are fetching only once that will return only first row.
Instead you must fetch all the rows using fetch statement in loop
while($row_cat = mysql_fetch_array($cat)){
echo $row_cat['title'];
echo $row_cat['description'];
}
From PHP mysq_fetch_array documentation:
"Returns an array that corresponds to the fetched row and moves the internal data pointer ahead"
As far as I know, you cannot retrieve every row without using a loop. You should do something similar to this:
while($row_cat = mysql_fetch_array($cat)){
echo $row_cat['title'];
echo $row_cat['description'];
}