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.
Related
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'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
Can any one help me? I found difficult about create matrix dynamic using PHP programming. I want value of matrix dynamic from mysql database.
Such as example :
INF 56 78 67
45 INF 78 67
45 56 INF 67
45 56 78 INF
This code it :
function random()
{
$max = 7;
if (isset($_POST['amount']))
{
$amount = (int)$_POST['amount'];
$rows = ($amount > 2 && $amount < 15) ? $amount : 3;
}
else
{
$rows = rand(3, 7);
}
for ($i = 1; $i <= $rows; $i++)
{
for ($j = 1; $j <= $rows; $j++)
{
$this->table[$i][$j] = $i == $j ? INF : rand(0, 99);
}
}
}
function __toString()
{
$str = '<table class="table table-bordered" id="tableInput"><tbody>';
$str .= '<tr><td></td>';
foreach ($this->table as $rowName => $row)
{
$str .= "<td>$rowName</td>";
}
$str .= '</tr>';
foreach ($this->table as $rowName => $row)
{
$str .= "<tr><td>$rowName</td>";
foreach ($row as $columnName => $value)
{
$str .= "<td>";
$str .=
'<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' .
$columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>';
$str .= "</td>";
}
$str .= '</tr>';
}
$str .= '</tbody></table>';
return $str;
}
Thanks
I have a follow up question on something I got help with here the other day (No Table Three Column Category Layout).
The script is as follows:
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$parent_node = mysql_fetch_assoc($res);
$id = (isset($parent_node['cat_id'])) ? $parent_node['cat_id'] : $id;
$catalist = '';
if ($parent_node['left_id'] != 1)
{
$children = $catscontrol->get_children_list($parent_node['left_id'], $parent_node['right_id']);
$childarray = array($id);
foreach ($children as $k => $v)
{
$childarray[] = $v['cat_id'];
}
$catalist = '(';
$catalist .= implode(',', $childarray);
$catalist .= ')';
$all_items = false;
}
$NOW = time();
/*
specified category number
look into table - and if we don't have such category - redirect to full list
*/
$query = "SELECT * FROM " . $DBPrefix . "categories WHERE cat_id = " . $id;
$result = mysql_query($query);
$system->check_mysql($result, $query, __LINE__, __FILE__);
$category = mysql_fetch_assoc($result);
if (mysql_num_rows($result) == 0)
{
// redirect to global categories list
header ('location: browse.php?id=0');
exit;
}
else
{
// Retrieve the translated category name
$par_id = $category['parent_id'];
$TPL_categories_string = '';
$crumbs = $catscontrol->get_bread_crumbs($category['left_id'], $category['right_id']);
for ($i = 0; $i < count($crumbs); $i++)
{
if ($crumbs[$i]['cat_id'] > 0)
{
if ($i > 0)
{
$TPL_categories_string .= ' > ';
}
$TPL_categories_string .= '' . $category_names[$crumbs[$i]['cat_id']] . '';
}
}
// get list of subcategories of this category
$subcat_count = 0;
$query = "SELECT * FROM " . $DBPrefix . "categories WHERE parent_id = " . $id . " ORDER BY cat_name";
$result = mysql_query($query);
$system->check_mysql($result, $query, __LINE__, __FILE__);
$need_to_continue = 1;
$cycle = 1;
$column = 1;
$TPL_main_value = '';
while ($row = mysql_fetch_array($result))
{
++$subcat_count;
if ($cycle == 1)
{
$TPL_main_value .= '<div class="col'.$column.'"><ul>' . "\n";
}
$sub_counter = $row['sub_counter'];
$cat_counter = $row['counter'];
if ($sub_counter != 0)
{
$count_string = ' (' . $sub_counter . ')';
}
else
{
if ($cat_counter != 0)
{
$count_string = ' (' . $cat_counter . ')';
}
else
{
$count_string = '';
}
}
if ($row['cat_colour'] != '')
{
$BG = 'bgcolor=' . $row['cat_colour'];
}
else
{
$BG = '';
}
// Retrieve the translated category name
$row['cat_name'] = $category_names[$row['cat_id']];
$catimage = (!empty($row['cat_image'])) ? '<img src="' . $row['cat_image'] . '" border=0>' : '';
$TPL_main_value .= "\t" . '<li>' . $catimage . '' . $row['cat_name'] . $count_string . '</li>' . "\n";
++$cycle;
if ($cycle == 7) // <---- here
{
$cycle = 1;
$TPL_main_value .= '</ul></div>' . "\n";
++$column;
}
}
if ($cycle >= 2 && $cycle <= 6) // <---- here minus 1
{
while ($cycle < 7) // <---- and here
{
$TPL_main_value .= ' <p> </p>' . "\n";
++$cycle;
}
$TPL_main_value .= '</ul></div>'.$number.'
' . "\n";
}
I was needing to divide the resulting links into three columns to fit my html layout.
We accomplished this by changing the numbers in the code marked with "// <---- here".
Because the amount of links returned could be different each time, I am trying to figure out how to change those numbers on the fly. I tried using
$number_a = mysql_num_rows($result);
$number_b = $number_a / 3;
$number_b = ceil($number_b);
$number_c = $number_b - 1;
and then replacing the numbers with $number_b or $number_c but that doesn't work. Any ideas?
As mentioned before, you can use the mod (%) function to do that.
Basically what it does is to get the remainder after division. So, if you say 11 % 3, you will get 2 since that is the remainder after division. You can then make use of this to check when a number is divisible by 3 (the remainder will be zero), and insert an end </div> in your code.
Here is a simplified example on how to use it to insert a newline after every 3 columns:
$cycle = 1;
$arr = range (1, 20);
$len = sizeof ($arr);
for ( ; $cycle <= $len; $cycle++)
{
echo "{$arr[$cycle - 1]} ";
if ($cycle % 3 == 0)
{
echo "\n";
}
}
echo "\n\n";
I'm making a calender planner with php/css/javascript/html.
I am trying to set each day of the month a class which is "day". I tried using the following:
**PHP CODE**
error_reporting(E_ALL);
ini_set('display_errors', '1');
include "scripts/connect_to_mysql.php";
$jobNameValue ='';
$monthDays = '';
$monthname = '';
$days = '';
//getting values for job names
$sql_job_name = mysql_query("SELECT * FROM jobs");
$num_job_name = mysql_num_rows($sql_job_name);
if($num_job_name >0) {
while ($row = mysql_fetch_array($sql_job_name)) {
$job_name = $row["job_name"];
$job_short_name = $row["job_short_name"];
$jobNameValue .= '<option value="' . $job_short_name . '">' . $job_name . '</option>';
}
} else {
$jobNameValue .= '<option value="NULL">No job listed</option>';
}
//getting values for months days
$sql_month_days = mysql_query("SELECT * FROM months");
$num_month_days = mysql_num_rows($sql_month_days);
if($sql_month_days > 0) {
while($row = mysql_fetch_array($sql_month_days)) {
$month_id = $row["id"];
$name = $row["name"];
$num_days = $row["num_days"];if($num_days === "31") {
for($i=1; $i <=31; $i++) {
$days .= '<span class="day">' . $i . '</span>';
}
} else {
if($num_days === "30") {
for($i=1; $i <=30; $i++) {
$days .= '<span class="day">' . $i . '</span>';
}
} else {
if($num_days === "29") {
for($i=1; $i <=29; $i++) {
$days .= '<span class="day">' . $i . '</span>';
}
}
}
}
$monthDays .= '<div id="monthContainer">
<span class="monthName">'. $name .'</span>
<div class="monthDaysContainter">
'. $days.'
</div>
</div>';
}
}
**HTML CODE**
<div id="container">
<div ="newJob">
<form action="<?php $_SERVER['PHP_SELF'] ?>" id="newJobForm">
<label for="jobName">Job Name:</label>
<select id="jobName">
<?php echo $jobNameValue; ?>
</select>
<input type="date">
<input type="submit" id="submit">
</form>
</div>
<div id="calander">
<?php echo $monthDays; ?>
</div>
</div>
It does not work correctly, it kept duplicating every month and I am not sure how to correctly right the php to achieve what was intended.
I am only a beginner at php so I am not good at the moment
Could anyone help me with this?
If there any more information you need, please don't hesitate to ask.
Thank you in advance!
Chris
Try just doing this and seeing if that fixes it, you don't need all of those if statements
for($i=1; $i <= $num_days; $i++) {
$days .= '<span class="day">' . $i . '</span>';
}
do not use ===,use ==,maybe your $num_days is integer
Your problem is that you are creating the whole thing in the while loop, so the days that are appended from the last call are also appended to the next output.
$tmpArray = array();
if($sql_month_days > 0) {
while($row = mysql_fetch_array($sql_month_days)) {
array_push($tmpArray, $row);
}
}
foreach($tmpArray as $k){
$monthDays .= '<div id="monthContainer"><span class="monthName">'. $k['name'] .'</span><div class="monthDaysContainter">'. calNumDays($k['num_days']).'</div></div>';
}
Update
Should you wish to keep what you already have
if($num_days === "31") {
$days = '';
for($i=1; $i <=31; $i++) {
$days .= '<span class="day">' . $i . '</span>';
}
} else {
if($num_days === "30") {
$days = '';
for($i=1; $i <=30; $i++) {
$days .= '<span class="day">' . $i . '</span>';
}
} else {
if($num_days === "29") {
$days = '';
for($i=1; $i <=29; $i++) {
$days .= '<span class="day">' . $i . '</span>';
}
}
}
}
You only really to need to reinitialize the $days before each for after the if