I am trying to create custom function for Main Nav menu where I can only write menu item name and it will automatically wrap with my content
Also I want variable where I can define url for each menu item.
below is my code and giving me Parse error: syntax error, unexpected T_VARIABLE
function the_main_nav($navlinks){
echo '<nav>';
echo '<ul>';
$menuitem = $navlinks;
$pieces = explode("," $menuitem);
echo $pieces[0];
echo $pieces[1];
echo $menuitem;
echo '</ul>';
return $pieces;
}
------------------[Modified code]----------------------
function the_main_nav($navlinks){
echo '<nav>';
echo '<ul>';
$menuitem = $navlinks;
$pieces = explode(" ",$menuitem);
echo '<li>';
echo $pieces[0];
echo '</li>';
echo '<li>';
echo $pieces[1];
echo '</li>';
echo '</ul>';
echo '</nav>';
}
Now I want to make it dynamic like instead of getting value from [0] [1]..so on i want it will automatic generate as per the input character and create list with li
You are missing the ,. You have to separate the argument of explode using comma.
$pieces = explode(",", $menuitem);
The explode() function breaks a string into an array.
explode(separator, string)
so in your code comma is missing.
Related
Not a php guru here.
I have a string:
works/but/needs/splitting
I'd need the output in a ul list
<ul>
<li>works</ul>
<li>but</li>
<li>needs</li>
<li>splitting</li>
<ul>
I have been looking into explode("/", $text); and tried
$originalstring = "works/but/needs/splitting";
$delimiter = "/";
if(strpos($originalstring,$delimiter) > 0){
But I just don't know much of php and I can't work it out
Sorry I might not understand your questions, But split by / is this
$originalstring = "works/but/needs/splitting";
$pieces = explode("/", $originalstring);
echo '<ul>';
foreach ($pieces as $pi){
echo '<li>'.$pi.'</li>';
}
echo '</ul>';
$originalstring = "works/but/needs/splitting";
$e=explode('/',$originalstring); //creates the array ($e) of each element
echo '<ul>';
foreach ($e as $each){ //loop the array ($e)
echo '<li>'.$each.'</li>';
}
echo '</ul>';
Ive been trying make this display as html list items it just a string that i explode then loop over each item i cant get it to out put correctly. Could some one please show me where im going wrong or suggest an new approch.
this is what ive tried
$path = "1/2/3/4";
$expath = explode("/",$path);
$ret = '';
echo '<ul>';
foreach ($expath as $pitem) {
echo '<li><a href='.$ret .= $pitem. "/".'>'.$pitem.'</a></li>';
}
echo '</ul>';
.
Desired out put on hrefs
1
1/2
1/2/3
1/2/3/4
Desired visual out LIs
1
2
3
4
Output i get be warned
1
12/>212/>23/>312/>23/>34/>4
$path = "1/2/3/4";
$expath = explode("/", $path);
echo '<ul>';
foreach ($expath as $i => $pitem) {
$slice = array_slice($expath, 0, $i + 1);
$path = implode('/', $slice);
echo '<li>' . $pitem . '</li>';
}
echo '</ul>';
$list = explode("/", "1/2/3/4");
This will create an array $list as:
echo $list[0]; // 1
echo $list[1]; // 2
echo $list[2]; // 3
echo $list[3]; // 4
This line is the problem: echo '<li><a href='.$ret .= $pitem. "/".'>'.$pitem.'</a></li>';
Should be formatted like:
echo "<li><a href='{$ret}={$pitem}/'>{$pitem}</a></li>";
or echo '<li>'.$pitem.'</li>';
Its because your $ret. Place that inside the loop. In your code you concatenate $pitem with $ret all older $ret values also get concat.
Try
<?php
$path = "1/2/3/4";
$expath = explode("/",$path);
echo '<ul>';
foreach ($expath as $pitem) {
$ret = '';
echo '<li><a href='.$ret .= $pitem. "/".'>'.$pitem.'</a></li>';
}
echo '</ul>';
If you want = sign tobe there in the url then just change echo by following
echo "<li><a href='$ret=$pitem/'>$pitem</a></li>";
PHP echo with double quotes will print variable value.
Just learning bits in PHP and trying to get simple function for my nav running
function navigation($pages) {
$pages = array($pages);
if($pages) {
echo "<ul class=nav>";
foreach($pages as $id => $page) {
echo "<li><a href=\"page.php?id={$id}\">";
echo strtoupper($page) . "</a>";
}
echo "</ul>";
}
}
Although this function is only returning the first value
navigation("Home", "About us", "Contact us");
Is it possible to put the function values to variable? I'm not sure what I'm doing here wrong.
Remove this:
$pages = array($pages);
And instead call the function like this:
navigation( array("Home", "About us", "Contact us") );
Please note that you should not output HTML like this:
echo "";
But instead make sure that the CSS classname is wrapped in quotes. To achieve this, you could use escaped double-quotes:
echo "";
Or, which would be my personal preference, single quotes in the outer string and normal double quotes inside:
echo '';
Generally you can use single-quotes to define any string in PHP, with only one exception, which is when you want to put special characters in your string, such as:
\n
\r
\t
and so on. These might not be interpreted correctly in a single-quoted string.
Here is a more refined version of your navigation() function:
function navigation(array $pages) {
if(!$pages)
return;
echo '<ul class="nav">';
foreach ($pages as $id => $page) {
echo '<li><a href="page.php?id=' . $id . '">';
echo strtoupper($page) . '</a>';
}
echo '</ul>';
}
<?php
function myFunction($text){
return md5($text);
}
$array = array(myFunction("Text to be MD5"), myFunction("More Sample Text"));
foreach($array as $value){
echo $value;
echo "<br>";
}
?>
Is this what you were looking for?
you can put the output of a function into an array
Output:
d41d8cd98f00b204e9800998ecf8427e
a95486400f22cfa1ce6ae3a8cacae1e4
What i'm trying to do is make my output usable for a spreadsheet.
I want each item in the output without array tags or not mashed together but starting with an asterisk and ending with a % sign.
<?php
$file = file_get_contents('aaa.txt'); //get file to string
$row_array = explode("\n",$file); //cut string to rows by new line
$row_array = array_count_values(array_filter($row_array));
foreach ($row_array as $key=>$counts) {
if ($counts==1)
$no_duplicates[] = $key;
}
//do what You want
echo '<pre>';
print_r($no_duplicates);
//write to file. If file don't exist. Create it
file_put_contents('no_duplicates.txt',$no_duplicates);
?>
Maybe this would give you what you want:
$str = "*" . implode("% *", $no_duplicates) . "%";
echo '<pre>';
echo $str;
echo '</pre>';
I have a field in my database with the text value:
"these, are, some, keywords" (minus the inverted commas)
Now, I wonder if I can generate an unordered list from this so ultimately my HTML reads:
<ul>
<li>these</li>
<li>are</li>
<li>some</li>
<li>keywords</li>
</ul>
Is this possible with PHP and if so is anyone able to help me out with this?
Many thanks for any pointers.
You can accomplish this with something like the following:
<?php
$yourList = "these, are, some, keywords";
$words = explode(',', $yourList);
if(!empty($words)){
echo '<ul>';
foreach($words as $word){
echo '<li>'.htmlspecialchars($word).'</li>';
}
echo '</ul>';
}
?>
As mentioned by elcodedocle, you may want to use str_getcsv() instead of explode if more appropriate.
Have a look at str_getcsv() and explode()
Example:
<?php
$mystring = "these, are,some , keywords";
$myvalues = str_getcsv($mystring);
$myoutput = "<ul>";
foreach ($myvalues as $value){
$myoutput .= "<li>".trim($value)."</li>\n";
}
$myoutput .= "</ul>";
echo $myoutput;
?>
You need to explode you string for ', '
print <ul>
for each element in the array you received you print '<li>' . $value . '</li>'
print </ul>
You can try:
$arr = explode(",","these, are, some, keywords");
$res = "<ul>";
foreach ($arr as $val){
$res .= "<li>" . $val . "</li>";
}
$res .= "</ul>";
echo $res;