I have MySQL variables, which are echoing but I want to use them in an array and randomly display the results - it's for a competition - the random is working great but I can't get the variable to show correctly when they're in the array().
I have written the following:
<?php
$answer = array('$this->item->correct_answer','$this->item->false_answer1','$this->item->false_answer2');
shuffle($answer);
?>
<ol class="answers">
<?php
for( $i = 0; $i < 3; $i++)
echo "<li>$answer[$i]</li>";
?>
</ol>
This is only outputting:
$this->item->correct_answer$this->item->false_answer2$this->item->false_answer1
That's because you've got ' for the array inputs. That means you're setting a string. Revise it to:
$answer = array($this->item->correct_answer,$this->item->false_answer1,$this->item->false_answer2);
As demonstrated On codepad.org
Related
I am trying to be able to make a loop that lets me place stars on a website, to show a rating (1 to 5) on the website, that you can put down with fields. However, I do not know how to convert the field correctly.
I am using WordPress with Custom Field types, this is were I define the variable.
I have a variable that I get from somewhere else, and I have tried the following two ways:
<?php
$star = intval(the_field('rating'));
for ($i=0; $i < $star; $i++){
intval(the_field('rating'));
}
?>
and the second one:
<?php
$star = number_format(the_field('rating');
for ($i=0; $i < $star; $i++){
number_format(the_field('rating'));
}
?>
Thank you very much for you help in advance.
First the_field echos the value, you need get_field to return the value. Then just repeat whatever string you're using to display a star or empty star:
$stars = get_field('rating');
echo str_repeat('*', $stars) . str_repeat('O', 5-$stars);
With a loop just check if your counter is less than or equal to the star rating:
$stars = get_field('rating');
for ($i=1; $i <= 5; $i++){
echo ($i <= $stars) ? "*" : "O";
}
I have a set of 12 circles, some will be blank, some will have an icons (this is driven by Wordpress whether they have an icon or not).
I then have a standard Wordpress loop looking for icons, if an icon is present then it will output. It also iterates the $counter variable starting at 1.
How can I take the count of that - then create a new loop to create blank circles?
So for instance, if 5 circles have icons then I would need 7 blank circles.
This is my attempt, at the moment it's creating an infinite loop. So from the example this needs to output 7, with the class name outputting the numbers 7, 8, 9 etc up to 12 to fill in the blanks.
Where am I going wrong?
<?php $final = 12 - $counter;
for($count = 1; $count <= $final; $count++) { ?>
<a class="research-circle blank-circle rs-<?php echo $final; ?>" href="#"></a>
<?php $final++; } ?>
It's infinite because you're increasing both values simultaneously.
Try this:
for($count = 1; $count <= $final; $count++) { ?>
<a class="research-circle blank-circle rs-<?php echo ($final + $count); ?>" href="#">
</a>
<?php }
?>
Look inside your loop condition: You tell PHP to loop until $count is greater than $final.
But in your loop, you increase both $count and $final value. $count will never be greater than $final.
I'm not very experienced in using PHP, but I'm having a hard time thinking of a way to do this. I'm reading from a file and exploding on ":" as you can see here.
<?php
$datainfo = file('data.txt');
for($i = 0; $i < count($datainfo); $i++){
$expdata = explode(':', $datainfo[$i]);
}
?>
The issue is that I need to reference specific indexes of the resulted explosion like this.
<p> <?php echo $expdata[1] ?> </p>
I'm getting back an array of the last line inside the data.txt file. I know why It's happening, I just don't know how to get what I want here. (Sorry Very New). Data.txt contain the following.
name:Octopod Juice Stand
balance:20
price:0.5
customers:12
starting:2014-05-26
end: 2014-09-01
juice:15.25
fruit:10
Change your code to
<?php
$datainfo = file('data.txt');
$expdata = array();
for($i = 0; $i < count($datainfo); $i++){
$expdata[] = explode(':', $datainfo[$i]);
}
?>
And then to get the first label.
<p><?php echo $expdata[0][0]; ?></p>
Or the first value
<p><?php echo $expdata[0][1]; ?></p>
I have a multiple checkbox in my html table with $number as index
">
I get the value of the multiple checkbox:
$checklist = $_POST['checkbox'];
The value of the $checklist are correct when i do echo.
$i = $checklist[0];
echo $i; // 2 for example
Now i try to get the node number $i (2) in my xml file
$participants = simplexml_load_file($fileName);
Then this line echo $participants->participant[$i]->name; display blank
Or if define $j = 2; then echo $participants->participant[$j]->name; display the name of the participant 3 ($j + 1) and that is correct.
That mean $check or $i = $checklist[0]; don't give a simple number like $j, any idea please ? Thanks in advance.
The problem is just because php type the variable, so $check is not int type, $j is int type.
Just do $i= (int)$check and it works well when doing $participants->participant[$i]->name;
Thansk for your participation.
Dont know much PHP and wonder how I can get the new values from an array each time I submit, I have a form with 5 fields, the form sends the information to a txt-file then I want to take only the two first items each time the form submits and display it in a list item, but cant seem to figure out how the loop should work? any tips please?
For now I have this and it successfully prints out the fist textblock in the txt-file, but when I submit the form again nothing happens, what is wrong?
$myFile = 'demo.txt';
$content = file_get_contents('demo.txt');
$content_array = explode(";", $content);
<div id="info_php">
<ul id="list_php">
<?php for($i=count($content_array); $i < 1; $i--);
echo '<li>'; echo $content_array[0]; echo'</li>';?>
</ul>
</div>
You have an error in syntax, the for loop is terminated by semicolon, and you are printing only the first element as $content_array[0], instead of using the loop variable. Try this:
<?php
for($i=count($content_array); $i < 1; $i--) {
echo '<li>'; echo $content_array[$i]; echo'</li>';
}
?>
The first thing you need to do is change your loop like this:
<?php
$count = count($content_array); // total rows
$limit = $count > 2 ? $count - 2 : 0; // at most we want two rows
for ($i = $count; $i > limit; $i--) {
echo '<li>' . $content_array[$i - 1] . '</li>';
}
?>
This will get the last two rows of the txt file
This is it!
<?php
$myFile = 'demo.txt';
$content = file_get_contents($myFile);
$content_array2 = explode("\n", $content);
?>
<?php for($i = 0; $i < count($content_array2); $i++):
$values = explode(';', $content_array2[$i]);?>
<li><?php echo $values[0].$values[1]; ?></li>
<?php endfor; ?>