For some reason my $count displays the number 1 first instead of 0 I really don't know what's going on I want count to start at 0 and display 0 first then 1, 2 and so on can someone help me correct this problem?
PHP code.
function make_list ($parent = 0, $count = 0, $depth = 0) {
global $link;
if($count == 0){
echo '<ol class="cat-width">';
} else {
echo '<ol class="cat-depth">';
}
foreach ($parent as $id => $cat) {
if($cat['parent_id'] == '0'){
echo '<li><input type="checkbox" name="cat[]" id="cat-' . $cat['id'] . '" value="' . $cat['id'] . '" />
<label for="cat-' . $cat['id'] . '" class="label-headers">' . $cat['category'] . '</label>';
} else {
$indent = str_repeat(' ', $depth * 3);
echo '<li>' . $indent . '<input type="checkbox" name="cat[]" id="cat-' . $cat['id'] . '" value="' . $cat['id'] . '" />
<label for="cat-' . $cat['id'] . '">' . $cat['category'] . '</label>';
}
if (isset($link[$id])) {
make_list($link[$id], $count+1, $depth+1);
}
echo '</li>';
}
echo '</ol>';
}
$dbc = mysqli_query($mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC");
if (!$dbc) {
print mysqli_error();
}
$link = array();
while (list($id, $parent_id, $category, $depth) = mysqli_fetch_array($dbc)) {
$link[$parent_id][$id] = array('parent_id' => $parent_id, 'id' => $id, 'category' => $category, 'depth' => $depth);
}
make_list($link[0], $depth+1);
The first call to make_list should be:
make_list($link[0],0, $depth+1);
rather than
make_list($link[0], $depth+1);
You are trying to give default value to $count only without giving default value to $depth. That is not possible.
Your call make_list($link[0], $depth+1); is actually assigning $depth+1 to $count and using the default value of 0 for $depth
try using null or 0 as second parameter and do check out func_get_args() http://php.net/manual/en/function.func-get-args.php. you wont need to set function parameters in this case. just call this function to get all the arguments you passed and manipulate it manually.
You're adding 1 to depth when calling the function. This means that inside the make_list function depth with only be 0 if called with a value of -1.
Your $count is nowhere displayed. Do you mean then 1. 2. ... from the <ol> ? As far as I can see it always starts with 1.
Related
I have these two associative arrays.
I want to print the matched category_id with CHECKED and the rest Un-CHECKED
Here is what I tried.
foreach ($all_available_categories as $category) {
if(in_array($category['category_id'], $current_item_categories)){
echo 'CHECKED';
}
echo '<input type="checkbox" name="item_categories_checkbox[]" value="' .$category['category_id'] . '">' . $category['category_name'] . "</br>";
}
You should do sometjhing like this:
Step1: Modify your $current_item_categories array like this:
$currentCategories = array();
foreach($current_item_categories as $currCat) {
array_push($currentCategories, $currCat['category_id']);
}
Step2: Now I am using the same logic as applied by you, with a very little modification.
foreach ($all_available_categories as $category) {
if(in_array($category['category_id'], $currentCategories)){
$checked = 'checked="checked"';
}else {
$checked = "";
}
echo '<input ' . $checked . 'type="checkbox" name="item_categories_checkbox[]" value="' .$category['category_id'] . '">' . $category['category_name'] . "</br>";
}
Note: code not tested.
Well, you could first make an associative array of current item categories, you just key it by the category id for fast lookup.
<?php
$currentCatIds = array();
foreach ($current_item_categories as $category) {
$currentCatIds[$category['category_id']] = 1;
}
foreach ($all_available_categories as $category) {
$catid = $category['category_id'];
// Forgot this one
$checked = isset($currentCatIds[$catId]) ? 'checked' : '';
echo '<input ' . $checked . ' type="checkbox" name="item_categories_checkbox[]" value="' . $catid . '" />' . $category['category_name'] . '<br />';
}
foreach ($all_available_categories as $category) {
foreach ($next_available_categories as $category1) {
if($category['category_id'] == $category1['category_id']){
echo 'CHECKED'; break;
}
}
echo '<input type="checkbox" name="item_categories_checkbox[]" value="' .$category['category_id'] . '">' . $category['category_name'] . "</br>";
}
The idea is really quite easy: loop all categories and then for each category check whether this category is checked. You are trying to do the latter using the PHP function in_array(), but that function doesn't search multi-dimensional. So that would only work if you would have an array of category id's: $ids = array('9', '13');, and then check the category id using in_array().
So if we make these modifications it works:
$ids = array();
foreach($current_item_categories as $category) {
$ids[] = $category['category_id'];
}
foreach ($all_available_categories as $category) {
if(in_array($category['category_id'], $ids)){
echo 'CHECKED';
}
echo '<input type="checkbox" name="item_categories_checkbox[]" value="' . $category['category_id'] . '">' . $category['category_name'] . '</br>';
}
An other way to do this would of course be to use two nested loops. This would no longer require an array of id's and allows for other checks to be made:
foreach ($all_available_categories as $category) {
foreach ($current_item_categories as $c) {
if ($category['category_id'] == $c['category_id']) {
echo 'CHECKED';
}
}
echo '<input type="checkbox" name="item_categories_checkbox[]" value="' . $category['category_id'] . '">' . $category['category_name'] . '</br>';
}
Finally, you should realize that your code doesn't check the checkboxes, it only prints the word "CHECKED". If that isn't what you want, you should add a checked attribute to your input element.
Credit to Frog's answer.
Here is the fully working code.
$current_ids = array();
foreach ($current_item_categories as $category) {
$current_ids[] = $category['category_id'];
}
foreach ($all_available_categories as $category) {
echo '<input type="checkbox" name="item_categories_checkbox[]" value="' . $category['category_id'] . '"';
if (in_array($category['category_id'], $current_ids)) {
echo ' CHECKED ';
}
echo ' >' . $category['category_name'] . '</br>';
}
$menu = array(
0 =>'top',
1 =>'photography',
2 =>'about'
);
<?php
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach( $menu as $key => $value)
{
$return .= '<a class="menu" href="index.php#' . $menu[$key] . '">' . $menu[$key] . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
?>
<?php echo main_menu($menu[1]); ?>
What i basically want to do is to pass a specific array value when i'm echoing out the menu.
I'm building a single page website with anchors and i want to pass value's so i can echo out the "top"-link.
I'm stuck at the point on how to pass the $key value trough the function.
**edit: I'm trying to print specific links. I want a function that is able to print out an link but i want to specify the link to print via the function argument.
for example:
<?php echo main_menu($key = '0'); ?>
result:
prints url: top
<?php echo main_menu($key = '2'); ?>
result:
prints url: photography
**
(A lack of jargon makes it a bit harder to explain and even harder to google.
I got my books in front of me but this is taking a lot more time than it should.)
You either need to pass the entire array and loop, or pass a single array item and not loop:
Single Item:
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
$return .= '<a class="menu" href="index.php#' . $menu . '">' . $menu . '</a>' . PHP_EOL .'';
$return .= '</div>';
return $return;
}
echo main_menu($menu[1]);
Entire Array:
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach($menu as $value) {
$return .= '<a class="menu" href="index.php#' . $value . '">' . $value . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
echo main_menu($menu);
You don't need $menu[$key] just use the $value.
Should you not just be using $value inside your loop? And passing the entire array rather than one item of the $menu array?
$menu = array(
0 =>'top',
1 =>'photography',
2 =>'about'
);
<?php
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach( $menu as $key => $value)
{
$return .= '<a class="menu" href="index.php#' . $value . '">' . $value . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
?>
<?php echo main_menu($menu); ?>
Try:
echo main_menu($menu); // You will get your links printed
Instead of
echo main_menu($menu[1]); // In this case error is occured like : **Invalid argument supplied for foreach**
NOTE: You can use $value instead of $menu[$key]
I am using a plugin for WordPRess called Easy Bootstrap Shortcodes to use Booystrap CSS within wordpress posts. Tabs are one feature I use a lot and when I use the tabs short code, it throws the following error:
Notice: Undefined index: tabs in /home/onedirec/public_html/tester/wp-content/plugins/easy-bootstrap-shortcodes/shortcode/tabs/plugin_shortcode.php on line 38
What's weird is that the tabs and all the information following is rendered apart from an indent that shouldn;t be there. You can see this in action here: http://onedirectionconnection.com/tester/?projects=take-me-home-tour
If anyone could help me figure out what the issue is, it would be greatly appreciated. Here's the code that's causing the error:
<?php
/* * *********************************************************
* jQuery UI Tabs
* ********************************************************* */
$_oscitas_tabs = array();
function osc_theme_tabs($params, $content = null) {
global $_oscitas_tabs;
extract(shortcode_atts(array(
'id' => count($_oscitas_tabs),
'class' => ''
), $params));
$_oscitas_tabs[$id] = array();
do_shortcode($content);
$scontent = '<ul class="nav nav-tabs " id="oscitas-tabs-' . $id . '">' . implode('', $_oscitas_tabs[$id]['tabs']) . '</ul><div
class="tab-content">' . implode('', $_oscitas_tabs[$id]['panes']) . '</div>';
if (trim($scontent) != "") {
$output = '<div class="' . $class . '">' . $scontent;
$output .= '</div>';
return $output;
} else {
return "";
}
}
add_shortcode('tabs', 'osc_theme_tabs');
function osc_theme_tab($params, $content = null) {
global $_oscitas_tabs;
extract(shortcode_atts(array(
'title' => 'title',
'active' => '',
), $params));
$index = count($_oscitas_tabs) - 1;
$pane_id = 'pane-' . $index . '-' . count($_oscitas_tabs[$index]['tabs']);
$_oscitas_tabs[$index]['tabs'][] = '<li class="' . $active . '"><a href="#' . $pane_id . '" data-toggle="tab">' . $title
. '</a></li>';
$_oscitas_tabs[$index]['panes'][] = '<div class="tab-pane ' . $active . '" id="'
. $pane_id . '">'
. do_shortcode
(trim($content)) . '</div>';
}
add_shortcode('tab', 'osc_theme_tab');
And here is line 38 on its own:
$pane_id = 'pane-' . $index . '-' . count($_oscitas_tabs[$index]['tabs']);
I know very little about PHP so if anyone can help me spot the error here, it would be greatly appreciated.
Well I'll just post the answer here since it worked in the comments.
Add this line just before line 38:
$_oscitas_tabs[$index]['tabs'] = array();
Right above this line:
$pane_id = 'pane-' . $index . '-' . count($_oscitas_tabs[$index]['tabs']);
That should stop the PHP notice from appearing.
You could try adding a # right before count()
$pane_id = 'pane-' . $index . '-' . #count($_oscitas_tabs[$index]['tabs']);
It keeps PHP from complaining.
I'm still a PHP noob, so I apologize if this is something simple.
I am creating a fairly basic search facility for a website using PHP and mySQL. I have connected to the database, selected the database, queried the table and have fetched the table columns;
$k = htmlspecialchars($_GET['k']); // Get search query
$select = mssql_query("SELECT * FROM search WHERE Title Like '%" . $k . "%'");
if( mssql_num_rows($select) < 1) {
$noResults = 'No results found for <b>' . $k . '</b>, <label for="k">Please try again.</label>';
} else {
while ($results = mssql_fetch_array($select)) {
$title = $results['Title'];
$link = $results['Link'];
$description = $results['Description'];
}
}
When I put the $results[''] columns into variables and then try to echo out each variable like so;
if( isset($noResults)) {
echo $noResults;
} else {
echo '<li>' . '<h2>' . '' . $title . '' . '</h2>' . '<p>' . $link . '</p>' . '<p>' . $description . '</p>' . '</li>';
}
it only echo's out one row matching that query however, If I was to just simple echo out the columns like so;
echo $results['Title'];
echo $results['Link'];
echo $results['Description'];
all rows matching the query will be displayed..
I'm not sure why this is happening. If someone could help me out that would be great!
You need to use a loop:
$k = mysql_real_escape_string($_GET['k']); // Get search query
$select = mssql_query("SELECT * FROM search WHERE Title Like '%" . $k . "%'");
if( mssql_num_rows($select) < 1) {
$noResults = 'No results found for <b>' . $k . '</b>, <label for="k">Please try again.</label>';
} else {
$results= array();
while ($result = mssql_fetch_array($select)) {
$results[]= $result;
}
}
if( isset($noResults)) {
echo $noResults;
} else {
echo "<ul>";
foreach($results as $result){
echo '<li>' . '<h2>' . '' . $result['title'] . '' . '</h2>' . '<p>' . $result['link'] . '</p>' . '<p>' . $result['description'] . '</p>' . '</li>';
}
echo "</ul>";
}
Do you execute the output in the while-loop?
If you execute the while-loop and call the echo after that, each resultset will overwrite the previous, and the echo will output the last resultset which was fetched.
If you call the echo in the Loop, every result set will generate "his own" output line.
If you want to hold every resultset in a variable you can use an array, which is declared in front of the loop and gets filled in the loop.
a few things are not clear from your question, but i am assuming that you are echo'ing the variables outside the loop since you are checking isset($noResults). that means you are reassigning the variables with new values in each loop of while. so ultimately you get the last one assigned to the variables. you have to either use an array to hold the values or echo it with in the loop.
I asked a similar question earlier but I'll ask it again in a different way because I re-worked the code a little.
I was wondering how can I indent categories and endless sub categories that I have in a select drop down menu using PHP & CSS?
Here is my PHP code to display the select drop down.
echo '<select name="parent_id">
<option value="0">None</option>';
function make_list ($parent) {
global $option;
foreach ($parent as $id => $cat) {
echo '<option value="' . $cat['id'] . '">' . $cat['category'] . '</option>';
if (isset($option[$id])) {
make_list($option[$id]);
}
}
}
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC");
if (!$dbc) {
print mysqli_error();
}
$option = array();
while (list($id, $parent_id, $category) = mysqli_fetch_array($dbc, MYSQLI_NUM)) {
$option[$parent_id][$id] = array('category' => $category, 'id' => $id, 'parent_id' => $parent_id);
}
make_list($option[0]);
echo '</select>';
Here is the output.
1. Apple
2. Arts & Entertainment
1. Amusement
2. Art
3. Artists
1. A
1. a1
2. a2
2. B
3. C
4. D
3. Automotive
4. Network
5. Server
6. Web Design
1. CSS
2. HTML
The numbers are just there to see the categories and sub categories easier.
I see you already have the recursion thing down -- try passing a "depth" parameter in your make_list function -- when you first call it, you'd set $depth to zero. Then, when you call it recursively, you'd say makelist($option[$id], $depth+1);
knowing what level of recursion you are on, you could easily construct a string of whitespace. Like:
$whitespace = str_repeat(' ', $depth);
for more indentation, try str_repeat(' ', $depth * 2); or similar
then just prepend your option with the whitespace.
You can try this, working example.
Seperate with -
echo '<select name="parent_id">
<option value="">Seçiniz</option>';
function make_list ($parent,$depth) {
global $option;
foreach ($parent as $id => $cat) {
$whitespace = str_repeat('-', $depth * 1);
echo '<option value="' . $cat['id'] . '">'. $whitespace . $cat['category'] . '</option>';
if (isset($option[$id])) {
make_list($option[$id], $depth+1);
}
}
}
$dbc = mysqli_query($vt,"SELECT * FROM menus");
$option = array();
while (list($id, $parent_id, $category) = mysqli_fetch_array($dbc, MYSQLI_NUM)) {
$option[$parent_id][$id] = array('category' => $category, 'id' => $id, 'parent_id' => $parent_id);
}
make_list($option[0], $depth = 0);
echo '</select>';