I have a page with 100 checkboxes.. on and off. These checkboxes are named "box1" "box2" "box3" etc. They are pulled from a single row/col in a table where I exploded 1|0|1|0|1 into individual checkbox inputs.
My issue is that when I try to update them, I can't figure out how I can get them back into a single string to put them back into the same column. Here is what I have.. but clearly it doesn't work. Some of this stuff is new to me. Any advice would help.
$i = 1;
while($i < 101){
$thisBox = $_POST['box'][$i];
if($thisBox != 1){$thisBox = 0;}
$boxData .= $thisBox . "|";
$i++;
}
It just shows up with 100x "|" and I am unsure if it's possible to create the $_POST['box##'] with each loop.
try $thisBox = $_POST['box'.$i]
try this,
$thisBox = $_POST["box$i"] == "on" ? 1 : 0;
$boxData .= $thisBox . "|";
No need of if condition
Related
I searched, but I couldn't find an answer anywhere here. I have a multiselect box that will take each selection and explode it into individual data entries into a MySQL DB. Here is the code
$arr = explode(",",$values["account_id"]);
$j = count($arr);
for($i = 0; $i < $j ; $i++)
{
$strInsert = "update accounts_data set balance=balance+".$values["debit"]." where id='".$arr[$i]."'";
DB::Query($strInsert);
}
The field account_id is an integer field, but if I have more than 2 items selected, the code breaks. If account_id is set to a VARCHAR, I can select everything and it works just as I need it to updating every account balance with the new debit. I'm not quite sure why it works for a VARCHAR and not an INT, and I can't quite figure out how to make it work after a month of trial and error. I was wondering if anyone had an idea that is smarter than I am.
not sure I understood about your explanation about varchar and int.
Here is a way I would do it:
$values["account_id"] = "30,56,78";
$values["debit"] = 100;
foreach(explode(",", $values["account_id"]) as $account)
{
$strInsert = "update accounts_data set balance=balance+" . $values["debit"] ." where id=" . $account;
echo $strInsert . PHP_EOL;
// DB::Query($strInsert);
}
Hope this helps something.
I need to populate an ecommerce feed with extraimages
here is the header :
|image_1|image_2|image_3|
I've a table where I store or not, extra images for one product.
So I make a request to see if there are some extra images, with an sql limit of 3 as I only want 3 images max in my feed.
What I need is to populate the feed with empty pipes if there are no images and I don't know the logic to do that.
The output would be, for example, like this if I have 2 extra images :
|url/image_1.jpg|url/image_2.jpg|| <- empty pipe to match the header of 3
or only one image :
|url/image_1.jpg|||
My code so far :
$products_extra_images_query = tep_db_query(
"SELECT products_extra_image, products_extra_images_id
FROM " . TABLE_PRODUCTS_EXTRA_IMAGES .
" WHERE products_id='" . (int)$row->id . "' LIMIT 3");
if (tep_db_num_rows($products_extra_images_query) >= 1){
// there are some extra_images
while ($extra_images = tep_db_fetch_array($products_extra_images_query)) {
$output .= $extra_images['products_extra_image']."|" ;
}
}
thanks for your help.
Sebastien
Create an array of 4 and put your image path into it, then implode it into a string separated by "|".
$images = array_fill(0,4,null);
$idx = 0;
if (tep_db_num_rows($products_extra_images_query) >= 1) {
// there are some extra_images
while ($extra_images = tep_db_fetch_array($products_extra_images_query)) {
$images[$idx] = $extra_images['products_extra_image'];
$idx++;
}
}
$output .= implode($images, '|');
If I understood correctly then you could try an approach like this ~ though using non-standard names such as tep_db_fetch_array perhaps confuse the issue?
$rows = tep_db_num_rows( $products_extra_images_query );
if ( $rows > 0 ){
$output=[];
/* add found records to the `output` array rather than a string - or, use `fetch_all` */
while( $extra_images = tep_db_fetch_array($products_extra_images_query)) {
$output[]=$extra_images['products_extra_image'];
}
/* an odd looking loop but should add extra `pipe` chars when there are not 3 results */
for( $i=0; $i < ( 3 - $rows ); $i++ )$output[]='';
echo '|' . implode('|',$output) . '|';
}
I have this. I'm trying to capture the SQL table header name from PHP. This works fine for me.
However I'm struggling to tune this to echo the whole list of table header names except one or two which I dont need to print.
Suppose the names of column number 10 and 15 do not need to be printed how do I tweak my attempt?
Here goes the the code thus far.
// DB1 Connection
$sql = "SELECT * FROM sal_vol;";
$result = mysqli_query($db1,$sql);
$i = 0;
while($i<mysqli_num_fields($result))
{
$meta=mysqli_fetch_field($result);
echo $i.".".$meta->name."<br />";
$i++;
}
while($i<mysqli_num_fields($result))
{
if ($i == 10 || $i == 15) continue;
$meta=mysqli_fetch_field($result);
echo $i.".".$meta->name."<br />";
$i++;
}
Hi I am very new to php and I am having 3 php arrays namely bookname, bookprice and bookisbn, I need to insert the values like
"bookisbn" "bookname" "bookprice" into mysql
eg:
isbn1 bookname1 bookprice1
isbn2 bookname2 bookprice2
isbn3 bookname3 bookprice3
As of now I tried to iterate the three arrays something like,
foreach($booknamearray as $bookname && $bookpricearray as $bookprice && $bookisbnarray as $bookisbn) { .. }
and
while($booknamearray as $bookname && $bookpricearray as $bookprice && $bookisbnarray as $bookisbn){ .. }
Nothing worked me, please kindly help me out to achieve this.
Thanks in advance,
Naveen.
Assuming they all have the same number of elements, you can use a for loop, and make a string of all the values:
for($i = 0; $i < count($booknamearray); $i++) {
$str = $booknamearray[$i] . " " . $bookpricearray[$i] . " " . $bookisbnarray[$i];
//Insert $str into db
}
While you don't say so explicitly, I'm guessing your 3 arrays have their various values in the SAME KEY, e.g.
$booknamearray[0] -> 'name1';
$bookisbnarray[0] -> 'isbn1';
$bookpricearray[0] -> 'price1';
In which case you can do
foreach($booknamearray as $key => $name) {
$isbn = $bookisbnarray[$key];
$price = $bookpricearray[$key];
etc...
}
I'm in a bit of pickle but the answer is probably pretty simple.
So I have my POST variable:
Array ( [accept] => accept,29 [accept1] => accept,30 [submit] => Save Selections )
That's just a print_r of $_POST.
Basically what I want to do is get the first variable, remove the 'accept,' part and store the number next to it in a variable, run some MySQL queries using that variable (containing that number) then move onto the next one; removing the 'accept,' storing the number next to it and running a query using that stored number. It want to do this for as many times is necessary to go through all the elements containing 'accept,' then a number.
Any help would be appreciated.
I was playing around with some ideas and have this code. It obviously doesn't work but perhaps I could fix and build on it?
while($i <= $elements)
{
while($x == 1)
{
$id = explode(',', next($_POST));
echo $id;
$x = 0;
}
$i++;
}
Im not sure this would fix your problem but I hope this helps and gives you an idea:
$i = 0;
while($i <= count($your_array))
{
$your_var = substr($your_array[$i], 7, 2); //get the number from your accept,29
$query = "SELECT * FROM TABLE WHERE COLUMN = ". $your_var; //use it to run queries as you said
$i+=1;
}
UPDATE - Im not sure if this would work
foreach($your_post_array as $items)
{
$your_var = substr($items, 7, 2);
$query = "SELECT * FROM TABLE WHERE COLUMN = ". $your_var; //use it to run queries as you said
$i+=1;
}
Maybe you should try to echo first the variables to make sure echo $your_var = substr($items, 7, 2);
I would use a foreach loop to loop through $_POST, check if the key starts with accept (stripos?), explode the value if it does and get the second value of the found array and store that somewhere for later use.
By the way, if possible I would change the front-end so that you have just one variable (an array) in $_POST you have to loop through.