I'm currently working on a school project about php. The task is to design a website with some php navigation, a calculator, a truthsayer and some other functions. Now, I have a problem with the truthsayer I would like to get some help with. The truthsayer is a php randomizer that prints out a "truth" from an array I've set up. The truthsayer also prints out different amounts of truths depending on what number I give it. For example: If I type "3" into a box and presses enter, it will post three truths.
Now the problem: Since it completely randomizes it's output, I tend to get the same truths after each other. I for example ask for 10 truths, and 4-5 of them are the same as one of the other truths. Therefore I was wondering if anyone had an idea on how to make it avoid posting the same truth over and over again. Someone I know suggested a temporary removal of the truth from the array, but I'm not sure how I will make that work.
Any ideas? Much appreciated=P
My code:
<html>
<body>
<div id="Sannsigar">
<?php
if(isset($_POST["psc"])) {
$adder1 = $_POST["i1"];
$SannOutput= "";
$Val = array("1=Ichi","2=Ni","3=San","4=Shi/Yon","5=Go","6=Roku","7=Nana","8=Hachi","9=Kyu","10=Jyu");
switch ($adder1) {
case "1" : {
for ($x=1; $x<=$adder1; $x++)
{
echo $Val[array_rand($Val)] . "<br>"; }
"<br>";
break;
}
case "2" : {
for ($x=1; $x<=$adder1; $x++)
{
echo $Val[array_rand($Val)] . "<br>"; }
break;
}
case "3" : {
for ($x=1; $x<=$adder1; $x++)
{
echo $Val[array_rand($Val)] . "<br>"; }
break;
}
case "4" : {
for ($x=1; $x<=$adder1; $x++)
{
echo $Val[array_rand($Val)] . "<br>"; }
break;
}
case "5" : {
for ($x=1; $x<=$adder1; $x++)
{
echo $Val[array_rand($Val)] . "<br>"; }
break;
}
case "6" : {
for ($x=1; $x<=$adder1; $x++)
{
echo $Val[array_rand($Val)] . "<br>"; }
break;
}
case "7" : {
for ($x=1; $x<=$adder1; $x++)
{
echo $Val[array_rand($Val)] . "<br>"; }
break;
}
case "8" : {
for ($x=1; $x<=$adder1; $x++)
{
echo $Val[array_rand($Val)] . "<br>"; }
break;
}
case "9" : {
for ($x=1; $x<=$adder1; $x++)
{
echo $Val[array_rand($Val)] . "<br>"; }
break;
}
case "10" : {
for ($x=1; $x<=$adder1; $x++)
{
echo $Val[array_rand($Val)] . "<br>"; }
break;
}
default: {
Echo "FEIL! Feil inndata. Oppgje eit tal mellom 1 og 10.";
break;
}
}
$Footer = fopen("Sannsigar/footer.php", "w");
fwrite ($Footer, "$adder1 sanning(ar) fr� Birger AS" . "<br>");
fclose($Footer);
}
?>
</div>
</body>
</html>
You don't need to define a switch case for each input. You can achieve the same thing using a little function.
function getRandom($adder1, $Val) {
$randoms = range(0, $adder1-1);
shuffle($randoms);
if ($adder1 <= count($Val)) {
foreach ($randoms as $r) {
echo $Val[$r]."\n";
}
}
}
The function can be called as below:
getRandom(3, $Val);
Sample Output:
9=Kyu
3=San
10=Jyu
Demo!
Related
I'm wondering if you guys can give me an idea about how to replace this each function:
while(list($key,$val) = each($_SESSION['cart'][$i]))
{
if ($key=="product_name"){
$product_name=$val;
}else if($key=="units"){
$units=$val;
}else if($key=="packing_size"){
$packing_size=$val;
}else if($key=="grade"){
$grade=$val;
}else if($key=="length"){
$length=$val;
}else if($key=="quantity"){
$quantity=$val;
}
}
echo $product_name."".$units."".$packing_size."".$grade."".$length."".$quantity;
echo "<br>";
}
Thanks!
I've try to use foreach, but isn't working at all
I assume your original question is "How to replace a deprecated loop with each()"...?
In this case ADyson's commentary will be of great help to you.
But then why carry out tests on keys to obtain values that you then place in variables that you only display...?
If you just want to display the values you can access your array through the keys
As in the code below
foreach($_SESSION['cart'][$i] as $key => $val)
{
echo $val["product_name"]."".$val["units"]."".$val["packing_size"]."".$val["grade"]."".$val["length"]."".$val["quantity"];
echo "<br>";
}
for a more elegant solution and better optimisation you can use foreach and a switch statement
foreach ($_SESSION['cart'][$i] as $key => $val) {
switch ($key) {
case "product_name":
$product_name = $val;
break;
case "units":
$units = $val;
break;
case "packing_size":
$packing_size = $val;
break;
case "grade":
$grade = $val;
break;
case "length":
$length = $val;
break;
case "quantity":
$quantity = $val;
break;
}
}
echo $product_name . "" . $units . "" . $packing_size . "" . $grade . "" . $length . "" . $quantity;
echo "<be>";
Working on a quiz website where when users submits their answers $perQuestionScore equals true when the answer is correct but false when it is wrong. I'm trying to find the instances where $perQuestionScore equals true in other to total the score but it doesn't seem to work.
My code looks like below
<?php
$perQuestionScore = 0;
if (isset($_POST['grader'])) {
if(isset($_POST[$chosen]))
{
$choice= $_POST[$chosen];
if (strpos($choice, $correctOne) !== false) {
$perQuestionScore++;
echo $_POST[$chosen] . "" . " is the correct answer";
} elseif (strpos($choice, $correctOne) == false) { echo $_POST[$chosen] . "" . " is the Wrong answer";
} else {
echo "You did not choose an answer"; {
}
}
}
}
}
echo "<input id=grader' type='submit' name='grader' value='Grade Quiz'>" . "</form>";
echo $perQuestionScore * 10;
}
$conn->close();
?>
I suggest simplifying it to this:
<?php
$perQuestionScore = 0; //initialize var
if ($choice == $correct) {
$perQuestionScore++; //add 1 if correct
}
echo $perQuestionScore * 10; //i guess you want it times 10
I am new in the world of web development.
I am trying to redirect the user to a link based on the some conditions using IF statement.
but the inside the IF block href is not being recognized.
Any advise so as how can I accomplish this ?
Thanks
EDIT 1
<?php if ($len == 12){
track;
}
else {
track;
}
?>
Use concatenation. It sure makes things easier.
<?php
$url1 = 'track';
$url2 = 'track';
if ($len == 12) {
echo $url1;
} else {
echo $url2;
}
?>
try this:
<?php
if ($len == 12){
echo "<a href='http://www.dddd.in/" . $row_Recordset1['TRACKING_NO'] . "1213'> track </a>";
} else {
echo "<a href='http://www.aaa.in/" . $row_Recordset1['TRACKING_NO'] . "1231'> track </a>";
}
?>
I have written some script that works great, BUT I have tried to add an elseif statement in which appears to do absolutely nothing, can't figure out where I am going wrong. (Yes I am a PHP newbie, but I am trying! :)
$i = 0;
foreach($arrJobs as $job)
{
echo "<div class=\"job-ind\">\n";
echo '<p>' . $job->title . ', ' . $job->location . "</p>\n";
echo '<p class="smaller">Reference: ' . $job->reference . "</p>\n";
echo '<p><strong>' . $job->salary . "</strong></p>\n";
echo "</div>\n";
if (++$i == 5) break;
elseif ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
}
Everything up the elseif statement works perfectly.
Must be something simple I am missing or misunderstanding!
$i will never be 0 at this location.
You increase it in the if-statement before the test (++$i).
Note that elseif and else if will only be considered exactly the same when using curly brackets.
if (++$i == 5) break;
elseif ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
Won't work, change it to:
if ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
elseif (++$i == 5) {
break;
}
elseif vs. else if
I see a lot of people discussing this in the thread.
These are both correct and will both work.
The difference being :
else if() {
}
Translates to:
else {
if(){
}
}
And can't be used in code blocks using colons like this:
if:
stuff
else if()://won't work
//stuff
endif;
basic syntax error ? you can't use the non braced short if's when doing an else if
if (++$i == 5) {
break;
} else if ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
notice the colour highlighting on this block compared to yours
Here is the working outcome, thanks for all your help!
$i = 0;
foreach($arrJobs as $job)
{
echo "<div class=\"job-ind\">\n";
echo '<p>' . $job->title . ', ' . $job->location . "</p>\n";
echo '<p class="smaller">Reference: ' . $job->reference . "</p>\n";
echo '<p><strong>' . $job->salary . "</strong></p>\n";
echo "</div>\n";
}
if ($i = 0) {
echo "<div class=\"job-ind\">\n";
echo '<img src="img/no-vac.png"/>';
echo "</div>\n";
}
else if (++$i == 5) {
break;
}
I made listview in jQuery Mobile and I used PHP to feed its contents. It works properly, but the code is too long and most part of it are very similar to each other. Is there any way to simplify the code? Please have a look at the code, then I'll explain what I really need to do:
<ol data-role="listview">
<?php
while ($row = mysql_fetch_array($result)){
echo "<li><a href=\"#\">";
// first column check
switch ($row[1]) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
}
// second column check
switch ($row[2]) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
default:
echo " N . ";
}
// third column check
switch ($row[3]) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
default:
echo " N . ";
}
// fourth column check
switch ($row[4]) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
default:
echo " N . ";
}
// fifth column check
switch ($row[5]) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
default:
echo " N . ";
}
// sixth column check
switch ($row[6]) {
case "Behnam":
echo " B ";
break;
case "Tarin":
echo " T ";
break;
default:
echo " N ";
}
echo "</li></a>";
}
?>
</a></li>
</ol>
and the result is:
By using while ($row = mysql_fetch_array($result)){ I can fetch each row of the sql table one by one. But I also need to check the value of each cell(column) as well.
Group the code in a 2 parameter function:
function checkRowValue($row, $checkDefault) {
switch ($row) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
default:
if ($checkDefault)
echo " N . ";
}
}
Invoke as:
<ol data-role="listview">
<?php
while ($row = mysql_fetch_array($result)){
echo "<li><a href=\"#\">";
// first column check
for ($i = 0; $i < 7; $i++)
checkRowValue($row[i], $i > 0);
}
This generalizes it by applying a function over all the columns in your row (skipping first) and then string them together with " . ".
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<li><a href=\"#\">";
echo join(' . ', array_map(function($v) {
if ($v == 'Behnam') {
return 'B';
elseif ($v == 'Tarin') {
return 'T';
else {
return 'N';
}
}, array_slice($row, 1));
echo "</li></a>";
}
Try This
if(in_array("Behnam",$row) {
echo " B . ";
}
else if(in_array("Train",$row) {
echo " T . ";
}
else {
echo " N . ";
}
This is the final result:
<ul id="competition_list" data-role="listview" data-inset="true" data-theme="a">
<?php
// using the returned value from database to feed the list.
// showing the competiton rounds' outcome.
while ($row = mysql_fetch_array($result)){
echo "<li><a href=\"#\">";
echo $row[0] . "."; //this is the id column in the table and will give me the number of the row
for($i = 1; $i <= 5; $i++){
switch ($row[$i]) {
case "Behnam":
echo " B ";
break;
case "Tarin":
echo " T ";
break;
}
} // end for()
echo "</a></li>";
} // end while()
?>
</ul>
<?php
while ($row = mysql_fetch_array($result)){
echo "<li><a href=\"#\">";
// first column check
foreach($row as r)
{
switch (r) {
case "Behnam":
echo " B . ";
break;
case "Tarin":
echo " T . ";
break;
}
}
?>