PHP array question? - php

Trying to stop the words has been entered from repeating when the array displays its contents when echoed.
Here is the code.
echo "$array[$x] has been entered";

echo implode(", ", $array) . " has been entered";

uh, echo $array[$x];?

??
how about
echo $array[$x]." has been entered";

As far as i could get your question; try below code:
foreach($array as $key => $value)
{
// if this is the first item
if ($key === 1)
{
echo "$array[$key] has been entered";
}
else
{
echo $array[$key];
}
}

After your updated comment:
You can use something like
foreach ($array as $a) {
echo $a;
}
echo "has been entered";

Use single-quotes rather than double-quotes:
echo implode(', ', $array) . ' has been entered';

Related

How to replace a string with a foreach loop

I want to replace a string with a loop in php
This is the string:-
$name=array("tom","vicky","raj");
str="{$loop_start} Good morning {$value} {$loop_end}";
i want it as:
foreach($name as $value){
echo Good morning $value;
}
Want to replace {$loop_start} with foreach($name as $value){
and {$loop_end} with }
I am trying but not getting the solution of it
Please help me if somebody know.
Thanks
PHP explode() will convert String into an array.
$name ='"tom","vicky","raj"';
$ino_array = explode(',', $name);
foreach($ino_array as $value) {
echo 'Good morning '.$value.' <br />';
};
Use the below code $name='"tom","vicky","raj"' is a string.
<?php
$str='"tom","vicky","raj"';
$str = substr($str,1);
$str = substr($str,0,-1);
$name = explode('","',$str);
foreach($name as $value){
echo "Good morning $value<br/>";
}
?>
This will might help you.

What is the quickest way to randomize a data set?

For a multiple choice quiz application i would like to show the dummy answers with the correct answer. But with the correct answer being in a different position at each different question.
This is what i've tried but it doesn't seem to be working:
if ($question->type == 1)
{
echo "<div id='dummy_answers'>";
//Show Dummy
echo '<h3>Dummy Answers</h3>';
//Get Dummy Answers
$query = $this->test_model->getDummyAnswers($question->id);
$dummy_num = 1;
foreach ($query->result() as $row)
{
$rand_number = rand(1, 3);
if ($dummy_num == $rand_number)
{
$dummy_num = $rand_number + 2;
echo '<h4>Answer '.$dummy_num.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
//Now echo the real answer
echo '<h4>Answer '.$rand_number.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>'; //Get id's for each.echo $row->id;
}
else
{
echo '<h4>Answer '.$dummy_num.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
$dummy_num++;
}
}
echo '</div>';
echo ' <hr/>';
}
?>
You should use shuffle function.
In your case it will be:
if ($question->type == 1)
{
echo "<div id='dummy_answers'>";
//Show Dummy
echo '<h3>Dummy Answers</h3>';
//Get Dummy Answers
$query = $this->test_model->getDummyAnswers($question->id);
$answers=$query->result();
shuffle($answers);
foreach ($answers as $nr=>$row)
{
echo '<h4>Answer '.($nr+1).'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
}
echo '</div>';
echo ' <hr/>';
}
?>
Put the answers in an array the use shuffle
$random_array = shuffle($answers);
All you need to do is put the keys for your answers in an array and call shuffle(). Something like this:
$keys = array_keys($answers);
shuffle($keys);
for ($key in $keys) {
echo $answers[$key];
}
I would suggest putting all the answers into an array and using the shuffle() function to randomize them. Once they're shuffled, just iterate the array with a loop and build the markup.
You could put the results into an array (1 correct and 3 incorrect), then shuffle, then output them?
$answers = array();
array_push($answers, "answer1");
array_push($answers, "answer2");
array_push($answers, "answer3");
array_push($answers, "answer4");
shuffle($answers);
foreach ($answers as $answer) {
echo $answer;
}

GET key change how to show what is in the url

I want to display everything what is in a url.
So if someone use test.php?test=yes&no=why
i want to show
test = yes
no = why
but when someone use test.php?bla=blala
i want to show
bla = blala
Is this possible?
Pretty print the $_GET variable.
<?php print_r($_GET); ?>
Try this:
foreach ($_GET as $key => $value) {
echo $key . " = " . $value
}
use this php script:
foreach($_GET as $key => $value){
echo $key . " = " . $value;
}
<?php var_dump( $_GET ); ?>
will echo out everything inside $_GET
<?php var_dump( $_REQUEST ); ?>
will do everything in both a $_POST and an $_GET
You can also try
<?php
$string = "Url Variables <br />";
foreach( $_REQUEST as $key => $value ){
$string .= $key." = ".$value." <br />";
}
echo $string;
?>

Title only if foreach exists

I have the following code:
foreach ($model->details as $detail) {
if ($detail->Title) echo $detail->Title;
if ($detail->Info) echo '<br />'.$detail->Info;
}
Now I want to add something like this:
'<h2>Details</h2>'.'<hr />'
to the top of the output but only when the if's in the foreach clause are met. If neither of the if's is met I want nothing to display at all.
Can some help me tweak my code to accomplish this?
Like so?
foreach ($model->details as $detail) {
if ($detail->Title && $detail->Info) {
echo '<h2>Details</h2><hr />';
}
if ($detail->Title) echo $detail->Title;
if ($detail->Info) echo '<br />'.$detail->Info;
}
Addendum: I'd probably rather do something like this, to also avoid the uneccessary <br /> when there's no Title, but only Info
foreach ($model->details as $detail) {
$text = array();
if ($detail->Title) $text[] = $detail->Title;
if ($detail->Info) $text[] = $detail->Info;
if (!empty($text)) {
echo '<h2>Details</h2><hr />';
echo implode('<br />', $text);
}
}
You need to output to a string, or use the output buffering. The following example uses the string approach:
$sOutputHtml = NULL;
foreach ($model->details as $detail) {
if ($detail->Title) $sOutputHtml .= $detail->Title;
if ($detail->Info) $sOutputHtml .= '<br />'.$detail->Info;
}
if ($sOutputHtml !== NULL) {
echo '<h2>Details</h2>'.'<hr />' . $sOutputHtml;
}

PHP - How do I read all attributes of XML items using simpleXML?

I'm making a script, that reads through the passed XML file and displays the source code. I've got it almost done, but the item attributes .. I can't find a way to catch them. Here's the code:
$xml = simplexml_load_file("path/to/file.xml");
showTree($xml->children(), 0);
function showTree($value, $i) {
if($value == '') {
foreach($value as $name2 => $value2) {
echo str_repeat('--', $i)." <$name2> \n";
showTree($value2, ($i+1));
echo str_repeat('--', $i)." </$name2> \n";
}
} else { echo str_repeat('--', $i)." ".trim($value)."\n"; }
} // end: function
As I said, it works fine, but doesn't display the attributes, for example:
<item id=2>Item</item>
returns only the:
<item>Item</item>
Thanks for any responses, Mike.
Unless I missread your code something like this should probably be about right.
$xml = simplexml_load_file("path/to/file.xml");
showTree($xml->children(), 0);
function showTree($value, $i) {
if($value == '') {
foreach($value as $name2 => $value2) {
$attribsStr = '';
foreach($value2->attributes() as $attribName => $attribValue) {
$attribsStr .= $attribName . '="' . $attribValue . '"' . ' ';
}
echo str_repeat('--', $i)." <$name2 $attribsStr> \n";
showTree($value2, ($i+1));
echo str_repeat('--', $i)." </$name2> \n";
}
} else { echo str_repeat('--', $i)." ".trim($value)."\n"; }
} // end: function
Have a look at http://php.net/manual/en/simplexmlelement.attributes.php

Categories