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
Related
I want call a php custom function(writeMsg()) inside a <div> tag.
Below is a code sample of how to do it, but I'm not too sure if this adheres to the syntax of the language?
<?php
function writeMsg() {
echo "This is a test!";
}
echo "<div class='wrapper'>";
for( $i=0; $i < 6; $i++ ) echo "<div>$i **call writeMsg()**</div>";
echo "</div>";
?>
PHP is not recursively embeddable/executable. You cannot embed PHP code inside a PHP string and expect PHP to execute it. Just do:
echo "<div>" . writeMsg() . "</div>"
But note that your function is doing echo, which means it performs IMMEDIATELY output, and the above code sample will effectively execute as
writeMsg();
echo "<div></div>';
Your function should RETURN the string, not echo it.
First change your function to:
function writeMsg() {
return "This is a test!";
}
And then just change your echo to:
echo "<div>" . $i . " " . writeMsg(). "</div>";
First and foremost, forgive me if my language is off - I'm still learning how to both speak and write in programming languages. How I can retrieve an entire object from an array in PHP when that array has several key, value pairs?
<?php
$quotes = array();
$quotes[0] = array(
"quote" => "This is a great quote",
"attribution" => "Benjamin Franklin"
);
$quotes[1] = array(
"quote" => "This here is a really good quote",
"attribution" => "Theodore Roosevelt"
);
function get_random_quote($quote_id, $quote) {
$output = "";
$output = '<h1>' . $quote["quote"] . '.</h1>';
$output .= '<p>' . $quote["attribution"] . '</p>';
return $output;
} ?>
<?php
foreach($quotes as $quote_id => $quote) {
echo get_random_quote($quote_id, $quote);
} ?>
Using array_rand and var_dump I'm able to view the item in the browser in raw form, but I'm unable to actually figure out how to get each element to display in HTML.
$quote = $quotes;
$random_quote = array_rand($quote);
var_dump($quote[$random_quote]);
Thanks in advance for any help!
No need for that hefty function
$random=$quotes[array_rand($quotes)];
echo $random["quote"];
echo $random["attribution"];
Also, this is useless
<?php
foreach($quotes as $quote_id => $quote) {
echo get_random_quote($quote_id, $quote);
} ?>
If you have to run a loop over all the elements then why randomize hem in the first place? This is circular. You should just run the loop as many number of times as the quotes you need in output. If you however just need all the quotes but in a random order then that can simply be done in one line.
shuffle($quotes); // this will randomize your quotes order for loop
foreach($quotes as $qoute)
{
echo $quote["quote"];
echo $quote["attribution"];
}
This will also make sure that your quotes are not repeated, whereas your own solution and the other suggestions will still repeat your quotes randomly for any reasonably sized array of quotes.
A simpler version of your function would be
function get_random_quote(&$quotes)
{
$quote=$quotes[array_rand($quotes)];
return <<<HTML
<h1>{$quote["quote"]}</h1>
<p>{$quote["attribution"]}</p>
HTML;
}
function should be like this
function get_random_quote($quote_id, $quote) {
$m = 0;
$n = sizeof($quote)-1;
$i= rand($m, $n);
$output = "";
$output = '<h1>' . $quote[$i]["quote"] . '.</h1>';
$output .= '<p>' . $quote[$i]["attribution"] . '</p>';
return $output;
}
However you are not using your first parameter-$quote_id in the function. you can remove it. and call function with single parameter that is array $quote
Why don't you try this:
$quote = $quotes;
$random_quote = array_rand($quote);
$random = $quote[$random_quote];
echo '<h1>' . $random["quote"] . '.</h1><br>';
echo '<p>' . $random["attribution"] . '</p>';
Want to create a function:
echo get_random_quote($quotes);
function get_random_quote($quotes) {
$quote = $quotes;
$random_quote = array_rand($quote);
$random = $quote[$random_quote];
return '<h1>' . $random["quote"] . '.</h1><br>'.'<p>' . $random["attribution"] . '</p>';
}
First, you dont need the $quote_id in get_random_quote(), should be like this:
function get_random_quote($quote) {
$output = "";
$output = '<h1>' . $quote["quote"] . '.</h1>';
$output .= '<p>' . $quote["attribution"] . '</p>';
return $output;
}
And I cant see anything random that the function is doing. You are just iterating through the array:
foreach($quotes as $quote_id => $quote) {
echo get_random_quote( $quote);
}
According to http://php.net/manual/en/function.array-rand.php:
array_rand() Picks one or more random entries out of an array, and
returns the key (or keys) of the random entries.
So I guess $quote[$random_quote] should return your element, you can use it like:
$random_quote = array_rand($quotes);
echo get_random_quote($quote[$random_quote]);
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.
I have the following code:
while($row = mysql_fetch_array($result)){
$output_items[] = $row["title"]; } // while
print(implode("\n", $output_items));
Which does what it says and splits the array with a new line for each item.
But how do I do the same and allow formatting with i.e. I basically want to say
foreach of the $output_items echo "<div class=whatever>$output_items</div> etc etc
Tearing my hair out with this!
Many thanks for all help
Darren
foreach ($output_items as $oi){
echo "<div class=whatever>$oi</div>";
}
doesn't work? or i did not get what you are searching for
Pretty simple, to make it easier to read I'd do something like this:
while($row = mysql_fetch_array($result))
{
echo '<div class="whatever">';
echo $row["title"];
echo '</div>' . "\n";
} // while
Although you could still do this with your original code pretty easily:
while($row = mysql_fetch_array($result)){
$output_items[] = '<div class="whatever">' . $row["title"] . '</div>'; } // while
print(implode("\n", $output_items));
Rather than implode() them all with line breaks, use string interpolation to add them together:
$out_string = "";
// Loop over your array $output_items and wrap each in <div />
// while appending each to a single output string.
foreach ($output_items as $item) {
$out_string .= "<div class='whatever'>$item</div>\n";
}
echo $out_string;
I have a slight problem with the echo statement outputting wrongly. Forexample when i do
echo "<div id=\"twitarea\">" . fetchtwitter($rss) . "</div>";
it displays function output but OUTSIDE of twitarea div. What is the cause of this behavior perhaps syntax?
thanks in advance
Here is the actual function
require_once('includes/magpie/rss_fetch.inc');
$rssaldelo = fetch_rss('http://twitter.com/statuses/user_timeline/12341234.rss');
function fetchtwitter($rsskey){
foreach ($rsskey->items as $item) {
$href = $item['link'];
$title = $item['title'];
print "<li class=\"softtwit\">$title</li><br>";
} }
Simply :
<?php
echo "<div id=\"twitarea\">";
fetchtwitter($rss);
echo "</div>";
?>
fetchtwitter($rss) is echoing output (it doesn't return it).
With that you don't have to modify fetchtwitter().
fetchtwitter() probably does an echo() of its own, instead of returning the string. The function is executed while echo prepares the whole string for output, before the string is printed.
Does fetchtwitter(...) write the output directly to the browser instead of returning it? Try something like:
<?php
ob_start();
fetchtwitter($rss);
$twitter = ob_get_clean();
echo "<div id=\"twitarea\">" . $twitter . "</div>";
?>
Or if you can modify the source of fetchtwitter(), get it to concatenate and return the string instead of echoing it.
In case you didn't see my comment.
Try using a return in your fetchtwitter() function rather than the echo that you have in there.
you could try delimiters maybe it helps
$twitter = fetchtwitter($rss);
ob_start();
echo <<<HTML;
<div id="twitarea">$twitter</div>
HTML;
echo ob_get_clean();
update
You can modify your function like this too
require_once('includes/magpie/rss_fetch.inc');
$rssaldelo = fetch_rss('http://twitter.com/statuses/user_timeline/12341234.rss');
function fetchtwitter($rsskey){
$bfr ="";
foreach ($rsskey->items as $item){
$href = $item['link'];
$title = $item['title'];
$bfr .= "<li class=\"softtwit\"> target=\"_blank\">$title</li><br>";
}
return $bfr;
}