I am trying to iterate over two arrays using foreach and for loop but foreach not working!
I have two arrays $data
and $pageNumber
I need to iterate over both and store data in SQL accordingly, $data
contains image and $pageNumber
contains number of pages so count of both arrays always same.
whenever I am trying to store $data
elements with respect to $pageNumber
like there are 3 images in $data
and their serial number are 3,11,20 in $pageNumber
, this always result in first element(image) of $data
and numbers are 3,11,20 this means images are not iterating same images saved with different numbers.
How Will I save both Image and numbers serial wise simultaneously Like -
$data = ['image1','image2','image3'];
$pageNumber = [22,1,44]
result should be like - image1 saved with pagenumber 22, image2 saved with page number1... in SQL table field Image_name
and Image_name
<?php
$imagecount = 0;
foreach ($data as $base64_string) {
$newFileName = $id . "0" . $imagecount . ".jpg";
// replacing data:image/jpeg;base64, from post request
$base64_string = str_replace('data:image/jpeg;base64,', '', $base64_string);
$base64_string = str_replace(' ', ' ', $base64_string);
//base64 data which will store in file
$decoded = base64_decode($base64_string);
$imagecount ;
if (file_put_contents("/record/images/data/" . $imagedirectory . "/" . $newFileName, $decoded)) {
$pages = $_POST['pnumber'];
$pageNumber = explode(',', $pages);
for ($i = 0; $i < count($pageNumber); $i ) {
$insrecords =
"insert into records(
ArticleID, Page_Number,
imagedirectory,
Image_name,
) values (
'" .
$id .
"',
'" .
$pageNumber[$i] .
"',
'" .
$imagedirectory .
"',
'" .
$newFileName .
"',
)";
mysql_query($insrecords) or die(mysql_error());
}
} else {
echo "here";
die();
}
}
?>
CodePudding user response:
Thank you everyone for the help and Downvote :) I resolved this.
<?php
$imagecount = 0;
foreach ($data as $base64_string) {
$newFileName = $id . "0" . $imagecount . ".jpg";
// replacing data:image/jpeg;base64, from post request
$base64_string = str_replace('data:image/jpeg;base64,', '', $base64_string);
$base64_string = str_replace(' ', ' ', $base64_string);
//base64 data which will store in file
$decoded = base64_decode($base64_string);
$imagecount ;
if (file_put_contents("/record/images/data/" . $imagedirectory . "/" . $newFileName, $decoded)) {
$pages = $_POST['pnumber'];
$pageNumber = explode(',', $pages);
foreach ($pageNumber as $page) {
$insrecords =
"insert into records(
ArticleID, Page_Number,
imagedirectory,
Image_name,
) values (
'" .
$id .
"',
'" .
$page .
"',
'" .
$imagedirectory .
"',
'" .
$newFileName .
"',
)";
mysql_query($insrecords) or die(mysql_error());
}
} else {
echo "here";
die();
}
}
?>
CodePudding user response:
I think you're missing a closing bracket in the for loop. add another closing curly bracket after the mysql_query and try again.
Also I am noticing you are using an old deprecated function "mysql_query"
Also check for sql injection.
Also you are inserting inside a loop which might exhaust your database.
Last but not least, please ensure proper identation and formatting when posting on SO so we can help you easier, and also for yourself :)