I'm trying to GET all data from dynamic multidimensional form. Each column has different name and id like this :
$result2 = mysql_query("SELECT * FROM tempselect");
while($row = mysqli_fetch_row($result2))
{
for ($i = 0; $i < count($result2) ; $i++) {
echo "<tr>";
for ($j = 0; $j < 12 ; $j++) {
echo "<td><input type=\"text\" size=\"4\" name=\"" . $i++ ."[]\"id=\"" .$row[$j] ."\" value=" . $row[$j] . "></td>";
}
}
}
Code above works properly, each column got different name and id. the output is like this :
The purpose from this form is to edit data from database and use it for next process. Now i want to combine all data into 1 multidimensional array like this :
2 4 4 . . . 0
3 . . . . . 0
3 . . . . . 0
. . . . . . 0
. . . . . . 0
3 . . . . . 0
I tried using code below, but only the first column saved into $data :
for ($i=0; $i < count($_GET['0']); $i++) {
for ($j=0; $j < count($_GET['submit_edit']); $j++) {
$data =$_GET[$j];
}
}
$data = array();
for ($i=0; $i < count($_GET['0']); $i++) {
for ($j=0; $j < count($_GET['submit_edit']); $j++) {
$data[] =$_GET[$j];
}
}
This is just as you are doing i see $data has to be $data[] . However I dont know what rest you are doing. If this solves the problem then good, if doesnt then please make the question more clear related to $_GET['submit_edit'] and other $_GET's
Related
For my dynamic navigation I need some help, since I'm still new to PHP.
Using these functions, this file passes the values of the array to a json and a js file.
The correct indexing is particularly important for the function.
$_js_remc->remote_copy($renaming_js_file, $renaming_js_file_to);
$_json_remc->remote_copy($renaming_jsonfile, $renaming_jsonfile_to);
if ($_js_remc->remote_is_writeable($renaming_js_file) && $_json_remc->remote_is_writeable($renaming_jsonfile)) {
$i = 10000;
$menu_string = "document.getElementById('categories_menu').innerHTML='";
for ($z = 0; $z < count($_POST['menu_lvl1_text']); $z++) {
$i = $i + 100;
$menu_string .= '<li>' . htmlentities($_POST['menu_lvl1_text'][$z], ENT_QUOTES, "UTF-8") . '';
$linkname_lvl1 = ($_POST['menu_lvl1_text'][$z]);
$url_lvl1 = ($_POST['menu_lvl1_link'][$z]);
$menu_list[$i] = array($linkname_lvl1 => $url_lvl1);
if (isset($_POST['menu' . $z . '_lvl2_text']) && !empty($_POST['menu' . $z . '_lvl2_text'])) {
$i = $i +1;
$menu_string .= '<ul>';
for ($zs = 0; $zs < count($_POST['menu' . $z . '_lvl2_text']); $zs++) {
$menu_string .= '<li>' . htmlentities($_POST['menu' . $z . '_lvl2_text'][$zs], ENT_QUOTES, "UTF-8") . '';
$linkname_lvl2 = ($_POST['menu' . $z . '_lvl2_text'][$zs]);
$url_lvl2 = ($_POST['menu' . $z . '_lvl2_link'][$zs]);
$menu_list[$i] = array($linkname_lvl2 => $url_lvl2);
$i = $i + 1;
}
$menu_string .= '</ul>';
}
$menu_string .= '</li>';
}
$menu_string .= "';";
As you can see from the code, the json file logically logs the following values.
{
"10100": {
"Lvl_1_1": "Level_1_1"
},
"10101": {
"Lvl1_2": "Lvl1_2"
},
"10102": {
"Lvl1_3": "Lvl1_3"
},
"10203": {
"Lvl_2_1": "Lvl_2_1"
},
"10204": {
"Lvl2_2": "Lvl2_2"
}
}
Problem:
The Sublevel (for ex. "10102") is, of course, to be incremented, but only within it's category, after which the index for the next category shell resume, for example, "10200". Each category has different numbers of sublinks.
The output will look like:
<li id="u_1_0">
<span id="i_1_0"></span>
<ul class="sub">
<li id="u_1_2"><span id="i_1_2"></span></li>
<li id="u_1_3"><span id="i_1_3"></span></li>
Does someone know advice?
If you struggle only with resetting your counter $i, you could just reset the counter after the inner for loop:
if ($_js_remc->remote_is_writeable($renaming_js_file) && $_json_remc->remote_is_writeable($renaming_jsonfile)) {
$i = 10000;
$menu_string = "document.getElementById('categories_menu').innerHTML='";
for ($z = 0; $z < count($_POST['menu_lvl1_text']); $z++) {
$i = $i + 100;
$menu_string .= '<li>' . htmlentities($_POST['menu_lvl1_text'][$z], ENT_QUOTES, "UTF-8") . '';
$linkname_lvl1 = ($_POST['menu_lvl1_text'][$z]);
$url_lvl1 = ($_POST['menu_lvl1_link'][$z]);
$menu_list[$i] = array($linkname_lvl1 => $url_lvl1);
if (isset($_POST['menu' . $z . '_lvl2_text']) && !empty($_POST['menu' . $z . '_lvl2_text'])) {
$oldCounter = $i;
$menu_string .= '<ul>';
for ($zs = 0; $zs < count($_POST['menu' . $z . '_lvl2_text']); $zs++) {
$i = $i + 1;
[...use $i as before...]
}
$i = $oldCounter;
$menu_string .= '</ul>';
}
$menu_string .= '</li>';
}
$menu_string .= "';";
Notice that your ids are not unique. If there are 100 sublinks, you are in trouble!
However, I don't understand your second code block and how it is related to your question.
I'm trying to get search results to paginate if there are greater than 10 items found in the database. For some reason, even though the code recognises there are more than 10 items and creates links for subsequent pages, all search results are listed on the first page only. Anyone able to help please? Code is below:
for($i = 0; $i < $terms_count; $i++)
{
$search_terms_array[$i] = trim($search_terms_array[$i]);
${"query".$i} = $this->mysqli_link->query("SELECT prod_id, prod_tags FROM table WHERE prod_tags LIKE '%" . $search_terms_array[$i] . "%'");
if(${"query".$i}->num_rows < 1)
{
$zerocount++;
}
else
{
$rows = array();
while($row = ${"query".$i}->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
$search_id_results[] = $row['prod_id'];
}
}
}
if($zerocount == $terms_count)
{
echo $this->err_handle->fetch_error_text("search_terms_0_results");
return;
}
else
{
$search_results = array_values(array_unique($search_id_results));
$search_results_count = count($search_results);
$search_page_count = ceil($search_results_count / 10);
$search_page_first_result = ($search_page - 1) * 10;
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
for($i = 0; $i < $search_results_count; $i++)
{
$query = $this->mysqli_link->query("SELECT * FROM table WHERE prod_id='" . $search_results[$i] . "' LIMIT " . $search_page_first_result . ", 10");
while($row = $query->fetch_array())
{
echo "<h4>" . $row['prod_name'] . "</h4><p><img src=\"includes/images/product_images/" . $row['prod_category'] . "/" . $row['prod_pic_filename'] . "\" alt=\"\" width=\"150\" height=\"200\" /></p><p>Price: £" . $row['prod_price'] . "</p><p>" . $row['prod_code'] . "</p><input type=\"number\" name=\"prod_qty\" maxlength=\"2\" /><input type=\"submit\" name=\"add_to_basket\" value=\"Add To Basket\" /></form></p><p> </p><p> </p>";
}
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
}
Ok so I found a solution to this problem and the full function is now as follows:
public function product_search($search_terms, $search_page, $search_flag_check)
{
if($search_flag_check == "invalid")
{
echo $this->err_handle->fetch_error_text("invalid_ns_term");
return;
}
if($search_terms == "")
{
echo $this->err_handle->fetch_error_text("no_search_string");
return;
}
$search_terms = htmlspecialchars($search_terms);
$search_terms = $this->mysqli_link->real_escape_string(filter_var($search_terms, FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_FLAG_NO_ENCODE_QUOTES));
$search_terms_array = explode(" ", $search_terms);
$terms_count = count($search_terms_array);
$zerocount = 0;
$search_id_results = array();
for($i = 0; $i < $terms_count; $i++)
{
$search_terms_array[$i] = trim($search_terms_array[$i]);
${"query".$i} = $this->mysqli_link->query("SELECT prod_id, prod_tags FROM table WHERE prod_tags LIKE '%" . $search_terms_array[$i] . "%'");
if(${"query".$i}->num_rows < 1)
{
$zerocount++;
}
else
{
$rows = array();
while($row = ${"query".$i}->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
$search_id_results[] = $row['prod_id'];
}
}
}
if($zerocount == $terms_count)
{
echo $this->err_handle->fetch_error_text("search_terms_0_results");
return;
}
else
{
$search_results = array_values(array_unique($search_id_results));
$search_results_count = count($search_results);
$search_page_count = ceil($search_results_count / 10);
$search_page_first_result = ($search_page - 1) * 10;
$search_page_results_limit = 10;
if($search_page_first_result < 1)
{
$search_page_first_result = 1;
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
$search_page_upper_limit = $search_page_first_result + 9;
if(array_key_exists($search_page_upper_limit, $search_results))
{
$search_results_limit = $search_page_first_result + $search_page_results_limit;
}
else
{
end($search_results);
$search_results_limit = key($search_results);
reset($search_results);
}
for($i = $search_page_first_result; $i <= $search_results_limit; $i++)
{
$query2 = $this->mysqli_link->query("SELECT * FROM table WHERE prod_id='" . $search_results[$i] . "'");
$row = $query2->fetch_array();
echo "<h4>" . $row['prod_name'] . "</h4><p><img src=\"includes/images/product_images/" . $row['prod_category'] . "/" . $row['prod_pic_filename'] . "\" alt=\"\" width=\"150\" height=\"200\" /></p><p>Price: £" . $row['prod_price'] . "</p><p>" . $row['prod_code'] . "</p><input type=\"number\" name=\"prod_qty\" maxlength=\"2\" /><input type=\"submit\" name=\"add_to_basket\" value=\"Add To Basket\" /></form></p><p> </p><p> </p>";
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
}
}
It took a bit of thinking and I was about to give up, but then I thought about using the array keys to calculate the limits for each search page and after a bit of googling on relevant PHP array functions it all fell into place quite well.
I understand the code may not be very tidy right now and there may be ways to optimize/improve it, however for the time being it does the job.
I have a working code below. Basically, the code prints the options 50 times and 4 is the default selected option.
for ($i = 1; $i <= 50; $i++) {
if($i == 4){
echo '<option value="' . $i . '" selected>' . $i . '%</option>';
}else {
echo '<option value="' . $i . '">' . $i . '%</option>';
}
}
but every time i click the submit button, the option field resets back to the selected option even though the variables was captured properly.
It would be good if the option selected by the user is preserved after the button was clicked and the only time it reset back to the selected defaults is if the page was refreshed.
check if the value is sent by post if not assign 4 to the variable option
<form action="#" method="post" accept-charset="utf-8">
<select name="select">
<?php
if(isset($_POST['select']))
{
$option= $_POST['select'];
}
else
$option=4;
for ($i = 1; $i <= 50; $i++) {
if($i == $option){
echo '<option value="' . $i . '" selected>' . $i . '%</option>';
}else {
echo '<option value="' . $i . '">' . $i . '%</option>';
}
}
?>
</select>
<input type="submit" name="" value="Sumbit">
</form>
This should do it - it uses the value sent from the client if it exists, or 4 if not.
if (isset($_POST['selected_value'])) {
$selected = $_POST['selected_value'];
} else {
$selected = 4;
}
for ($i = 1; $i <= 50; $i++) {
if($i == $selected){
echo '<option value="' . $i . '" selected>' . $i . '%</option>';
}else {
echo '<option value="' . $i . '">' . $i . '%</option>';
}
}
Take value from posted field, say "select", if exist then take it as selected otherwise your default value.
Using below code:
$selected = (isset($_POST['select']) ? $_POST['select'] : 4);
for ($i = 1; $i <= 50; $i++) {
if($i == $selected){
echo '<option value="' . $i . '" selected>' . $i . '%</option>';
}else {
echo '<option value="' . $i . '">' . $i . '%</option>';
}
}
Hello I've got the following function:
function getProductInformation($productArr, $productNumber){
for ($i=0; $i < count($productArr[0]); $i++) {
$products[] = $productArr[0][$i];
}
for ($i=0; $i < count($productArr[1]); $i++) {
$sizes[] = $productArr[1][$i];
}
for ($i=0; $i < count($productArr[2]); $i++) {
$prices[] = $productArr[2][$i];
}
?><p><?php
return $products[$productNumber] . " " . $sizes[$productNumber] . " " . $prices[$productNumber];
?></p><?php
}
If i echo the function with an array and a number as params it will return the following:
<p>product size price</p>
Instead of the function only returning one paragraph i would like the result to look like this
<p>product</p> <p>size</p> <p>price</p>
All help appreciated!
Include HTML tags in strings:
function getProductInformation($productArr, $productNumber){
$products = array_values($productArr[0]);
$sizes = array_values($productArr[1]);
$prices = array_values($productArr[2]);
return "<p>" . $products[$productNumber] . "</p> <p>" . $sizes[$productNumber] . "</p> <p>" . $prices[$productNumber] . "</p>";
}
I'm looking to limit to the first 5 results returned here.
This works, but it does not limit the data set:
<?php
foreach($sxml->status as $status){
$name = $status->user->name;
$image =$status->user->profile_image_url;
$update =$status->text;
$url = "http://twitter.com/" .$status->user->screen_name;
echo "<li><img src=\"" . $image . "\" alt=\"" . $name . " image\" />" . $name . " " . $update . "</li>";
}
?>
I've tried this:
<?php
for($n = 0; $n <= 5; $n++){
$name = $sxml->$status[$n]->user->name;
$image = $sxml->$status[$n]->user->profile_image_url;
$update = $sxml->$status[$n]->text;
$url = "http://twitter.com/" . $sxml->$status[$n]->user->screen_name;
echo "<li><img src=\"" . $image . "\" alt=\"" . $name . " image\" />" . $name . " " . $update . "</li>";
}
?>
and am really kind of unsure why it doesn't work. If I simply do:
<?php echo $sxml->status[0]->user->name ?>
then I get the proper result. But when attempting it within the for loop, I get NULL.
Perhaps some kind of while? A different setup altogether? Thanks so much for any help you can give on this.
Change this:
for($n = 0; $n <= 5; $n++){
$name = $sxml->$status[$n]->user->name;
$image = $sxml->$status[$n]->user->profile_image_url;
$update = $sxml->$status[$n]->text;
$url = "http://twitter.com/" . $sxml->$status[$n]->user->screen_name;
echo "<li><img src=\"" . $image . "\" alt=\"" . $name . " image\" />" . $name . " " . $update . "</li>";
}
To this:
for($n = 0; $n <= 5; $n++){
$name = $sxml->status[$n]->user->name;
$image = $sxml->status[$n]->user->profile_image_url;
$update = $sxml->status[$n]->text;
$url = "http://twitter.com/" . $sxml->status[$n]->user->screen_name;
echo "<li><img src=\"" . $image . "\" alt=\"" . $name . " image\" />" . $name . " " . $update . "</li>";
}
You accidentally were writing this:
<?php echo $sxml->$status[0]->user->name ?>
Where it was trying to us $status[0] as a variable variable and of course, that doesn't exist and is thus undefined/null.
If you had something that works, why overcomplicate things by changing everything? Just limit the processing to the first N entries.
$i = 0;
foreach ($sxml->status as $status) {
if (++$i > 5) {
// stop after 5 loops
break;
}
// the rest is identical
}
Btw, $n = 0; $n <= 5; $n++ will limit to the first 6 entries, not 5.
$n = 0; $n < 5; $n++ will do what you asked for.
Don't you mean
$n = 0; $n < 4; $n++
I've also tried this, and it works great :-)
foreach ($xml->item as $item) {
if (++$i > 5) { break; }
$item->title . '';
} //foreach()
Note I'm not using $i = 0; it seems to know that by default ;-)
I hope this helps some one.