ARRAY_RAND always has the same output - php
well what it is is I have a task in php where I need to pick a card at random, then a button named "draw" which grabs another random card.
<td><b>Rectangle 10</b></br>
<p>Your card is: </p>
<?php
$cards = array("AC","AD","AH","AS","1C","1D","1H","1S",
"2C","2D","2H","2S","3C","3D","3H","3S","4C","4D","4H","4S",
"5C","5D","5H","5S","6C","6D","6H","6S","7C","7D","7H","7S",
"8C","8D","8H","8S","9C","9D","9H","9S","10C","10D","10H","10S",
"JC","JD","JH","JS","QC","QD","QH","QS","KC","KD","KH","KS"
);
$card = array_rand($cards, '1');
print_r($cards[$card]);
?>
<form>
<input type="button" value="Draw" onClick="window.location.reload()">
</form>
</td>
The code works on it's own, but in the whole page it doesn't, I'll paste the full page now.
<?php
$today = date_default_timezone_set('Europe/London');
$a = 3034;
srand($a);
?>
<table border='1'>
<tr>
<td><b>Rectangle 1</b></br>Rob Dorsett </br> 12007071 </br><?php echo $today = date('d/m/y')?></td>
<td><b>Rectangle 2</b></br><?php echo $addition = (3+4+5) ?></td>
</tr>
<tr>
<td><b>Rectangle 3</b></br> <?php srand(3034);
echo (rand()); ?> </td>
<td><b>Rectangle 4</b></br>
<?php for ($i=30;$i<=40;$i++){
if($i%2 == 0) {
echo $i. '</br>';}
}
?>
</td>
</tr>
<tr>
<td><b>Rectangle 5</b></br>
<form>
</br>
Enter four digits: </br>
<input name="input" />
</br><input type="submit" value="Sumbit" />
</form>
<?php
$x = $_GET['input'];
?>
</td>
<td><b>Rectangle 6</b></br>
<?php
if ($x%2 == 0){
echo 'Your number is even';
}
else
{
echo 'Your number is odd';
}
?>
</td>
</tr>
<td><b>Rectangle 7</b></br>
<form>
Enter a seed for a random number: </br>
<input type="text" name="random" /> </br>
<input type="submit" value="Generate" /> </br>
<?php
$r7 = $_GET['random'];
srand($r7);
echo rand();
?>
</form>
</td>
<td><b>Rectangle 8</b></br>
<?php
echo str_rot13('Example Rot13');
?>
</br>
<i>Example Rot13</i>
</td>
<tr>
<td><b>Rectangle 9</b></br>
<script type="text/javascript">
function rot13 (txt) {
var map = []
var tmp = "abcdefghijklmnopqrstuvwxyz"
var buf = ""
for (j = 0; j < tmp.length; j++) {
var x = tmp.charAt(j); var y = tmp.charAt((j + 13) % 26)
map[x] = y; map[x.toUpperCase()] = y.toUpperCase()
}
for (j = 0; j < txt.length; j++) {
var c = txt.charAt(j)
buf += (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' ? map[c] : c)
}
return buf
}
</script>
<script>var foo = rot13 ('Example Rot13')</script>
<p>Encoded: <script>document.write(foo)</script>
<p>Decoded: <script>document.write(rot13(foo))</script>
</td>
<td><b>Rectangle 10</b></br>
<p>Your card is: </p>
<?php
$cards = array("AC","AD","AH","AS","1C","1D","1H","1S",
"2C","2D","2H","2S","3C","3D","3H","3S","4C","4D","4H","4S",
"5C","5D","5H","5S","6C","6D","6H","6S","7C","7D","7H","7S",
"8C","8D","8H","8S","9C","9D","9H","9S","10C","10D","10H","10S",
"JC","JD","JH","JS","QC","QD","QH","QS","KC","KD","KH","KS"
);
$card = array_rand($cards, '1');
print_r($cards[$card]);
?>
<form>
<input type="button" value="Draw" onClick="window.location.reload()">
</form>
</td>
</tr>
</table>
You are feeding the srand() function with a static value, so you will always get the same result. Also, look at my last comment.
Removeing all srand(); makes your code work.
You should make some functions to get the random numbers and separate your PHP from your html/js. It would help the readabillity.
Related
How to display count data accumulation and residual data in table automatically?
I have a problem, I use the straight-line method when displaying the accumulation and residue. I wonder how can I use php to automate the calculation. This is the form used to submit the data: <h1>Penyusutan</h1> <table> <form action="deprecalc.php" method="post"> <label>Nominal : </label> <input type="text" name="nominal"><br> <label>Lama Depresiasi : </label> <input type="text" name="depresiasi"><br> <label>Awal : </label> <input type="text" name="awal"><br> <label>Akhir : </label> <input type="text" name="akhir"><br> <label>Residu Aset : </label> <input type="text" name="residu"><br> <input type="submit"> </form> </table> This is the php handling: <?php $nominal = $_POST['nominal']; $depresiasi = $_POST['depresiasi']; $awal = $_POST['awal']; $akhir = $_POST['akhir']; $residu = $_POST['residu']; ?> <h2>Nominal : <?php echo $nominal;?></h2></br> <h2>Despresiasi : <?php echo $depresiasi;?></h2></br> <table id="values"> <tr> <th>Nominal</th> <th>Tahun</th> <th>Umur</th> <th>Akumulasi</th> <th>Residu</th> </tr> <?php $no = 1; $years = date('Y'); for ($i=0; $i < $depresiasi ; $i++) { $rumus = ($nominal - $residu) / $depresiasi; $rumusResidu = ($nominal - $rumus); ?> <tr> <?php echo "<td>".$nominal."</td>" ?> <?php echo "<td>".$years++."</td>" ?> <?php echo "<td>".$no++."</td>" ?> <?php echo "<td>".$rumus."</td>" ?> <?php echo "<td>".$rumusResidu."</td>" ?> </tr> <?php } ?> </table> And the result I have made: Attached with the desired result: How do I achieve this? Thanks
Do while loop not looping in php
I've been working a loop that repeats incrementally, while displaying the kelvin and fahrenheit counterparts in a table. I'm using a do while loop to do so, however the function does not loop, and does not start on the correct number from the form. The code I've done so far is: <body> <form action="" method="post"> Start temperature in degrees:<input type="text" name="start"></input><br /> End temperature in degrees:<input type="text" name="end"></input><br /> How should the list be incremented?:<input type="text" name="inc"></input><br /> <input type="submit" name="sub" value="Submit"></input><br /> </form> <?php $a = 1; if ($_POST['sub']) { $a = 0; $start = $_POST['start']; $end = $_POST['end']; $inc = $_POST['inc']; $x = $start; do { $x = $x + $inc; $y = $x + 273; $z = (1.8 * $x) + 32; } while ($x >= $end); } ?> <?php if ($a != 1) { ?> <table> <tr> <th>Celsius</th> <th>Kelvin</th> <th>Fahrenheit</th> </tr> <tr> <th> <?php echo "$x degrees <br />"; ?> </th> <th> <?php echo "$y degrees <br />"; ?> </th> <th> <?php echo "$z degrees <br />"; ?> </th> </tr> </table> <?php } ?> </body> Would I have to include while loops in the echo part of the table for it to loop? And how would I be able to have the loop start on the same number as the form?
is it you need? <body> <form action="" method="post"> Start temperature in degrees:<input type="text" name="start"></input><br /> End temperature in degrees:<input type="text" name="end"></input><br /> How should the list be incremented?:<input type="text" name="inc"></input><br /> <input type="submit" name="sub" value="Submit"></input><br /> </form> <table > <tr> <th>Celsius</th> <th>Kelvin</th> <th>Fahrenheit</th> </tr> <?php $a = 1; if (isset($_POST['sub']) && $_POST['sub']) { $a = 0; $start = $_POST['start']; $end = $_POST['end']; $inc = $_POST['inc']; $x = $start; do { $x = $x + $inc; $y = $x + 273; $z = (1.8 * $x) + 32; ?> <?php if ($a != 1) { ?> <tr> <th><?php echo "$x degrees <br />"; ?></th> <th><?php echo "$y degrees <br />"; ?></th> <th><?php echo "$z degrees <br />";?></th> </tr> <?php } } while ($x >= $end); } ?> </table> </body>
Saving the output of calculation as the new input of a calculator
I want to make a calculator in PHP. I need a way to save the result of calculation as a new predefined input. Example: user multiplies 5 and 6 and gets a result of 30. If they want to multiply the result they presently need to input 30 again and choose a desired operation. I want to save the result of every operation so that the user can continue to do calculations with the result. If the user doesn't want to further use the result they just need to enter a new number and click submit. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Kalkulator</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <?php if (isset($_POST["posalji_vrijednosti"])) { $b_1 = $_POST["broj_1"]; $op = $_POST["operacija"]; $b_2 = $_POST["broj_2"];; } // Varijabla koja ce sadrzavati rezultat $rezultat = ""; // Program odradjuje svoje ispod if ($op == "*") { $rezultat = $b_1 * $b_2; } elseif ($op == "/") { $rezultat = $b_1 / $b_2; } elseif ($op == "+") { $rezultat = $b_1 + $b_2; } elseif ($op == "-") { $rezultat = $b_1 - $b_2; } ?> <form method="post"> <table> <tr> <td>Unesi prvi broj</td> <td><input type="text" name="broj_1"></td> </tr> <tr> <td>Odaberi operaciju</td> <td><select name="operacija"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> </td> </tr> <tr> <td>Unesi drugi broj</td> <td><input type="text" name="broj_2"></td> </tr> <tr> <td><input type="submit" name="posalji_vrijednosti" value=" = "></td> </tr> <tr> <td> <td> <?php echo "Rezultat: $rezultat"; ?> </td> </td> </tr> </table> </form> </body> </html>
If you want to serve the use with the calculated result as the new input, you just need to put the result as the new value of your input. The simplest solution & answer is: <input type="text" name="broj_1" value="<?php echo $rezultat; ?>"> If you want the user to be able to initially start from scratch you can still show it, but pre-select the result: <?php $rezultat = ""; // init that var // your calculation $rezultat = "30"; ?> <input type="text" id="inputField" value="<?php echo $rezultat; ?>"> <script> input = document.getElementById('inputField'); input.select(); </script> Or you can not show the result in input, but serve a button that will insert the result into that input: <?php $rezultat = ""; // init that var // your calculation $rezultat = "30"; ?> <input type="hidden" id="result" value="<?php echo $rezultat; ?>"> <button onClick="copyValue()">re-use Result</button><br> <input type="text" id="inputField"> <script> function copyValue() { result = document.getElementById('result'); input = document.getElementById('inputField'); input.value = result.value; } </script>
Query does not work
I made this code: Acum, bifeaza materiile pe care le studiaza clasa aleasa:<br /> <form name="servForm" action="<?php $PHP_SELF; ?>" method="post" > <table border="0"> <?php $a = 0; $rezultat = "SELECT id, materie FROM materii ORDER BY id"; $rezultat1 = mysql_query($rezultat); while($plm = mysql_fetch_array($rezultat1)) { if($a++ %5 == 0) echo "<tr>"; ?> <td align="center"><input type="checkbox" name="checkbox2[]" value="<?php echo $plm['id']; ?>" /></td> <td style="text-align:left"><?php echo $plm["materie"]; ?> </td> <?php if($a %5 == 0) echo "</tr>"; } ?> </table> </div> <br/> <input type="reset" value="Sterge" /> <input type="submit" value="Salveaza" name="savebtn" /> </form> <?php if(isset($_POST['savebtn'])) { foreach($_POST["checkbox2"] as $loc_id) { $query = "INSERT INTO materii_pe_clase(id_scoala,id_clasa,id_materie) VALUES('$scoalalui','$clasalui','$loc_id')"; //aici cauta ! :)) $result5 = mysql_query($query) or die('eroare'); }//sfarsit foreact }//sfarsit if isset Why does the last query not work? p.s. its a school project, so mysql is ok, no need for mysqli. p.p.s I defindet the $scoalalui and $clasalui somwhere a little up the page. but they are not the problem, i tried replacing them with values. the query simply does not work. thanks! thank you all! EDIT VARDUMP for $clasalui and $scoalalui : string '1' (length=1) string '1' (length=1)
Your problem here is that, you have error tool turned off, because PHP should have said, something like this. Notice: Undefined variable $PHP_SELF" Since you don't see it, I'd assume that, its a root of your "problem". PHP_SELF is not a variable, that's a constant. Its not even required here, as by default PHP sends data to its target URL. I improved readability of your code, so that should work for you now, <?php // You want to see all errors? Fine: error_reporting(E_ALL); $a = 0; $rezultat = "SELECT id, materie FROM materii ORDER BY id"; $rezultat1 = mysql_query($rezultat); // If the form is submitted, this will be executed if (isset($_POST['savebtn'])) { foreach($_POST["checkbox2"] as $loc_id) { $query = "INSERT INTO `materii_pe_clase` (`id_scoala`, `id_clasa`, `id_materie`) VALUES('$scoalalui', '$clasalui', '$loc_id')"; $result = mysql_unbuffered_query($query); if (!$result){ die(mysql_error()); } } // And finally die('Saved. Thanks'); } ?> Acum, bifeaza materiile pe care le studiaza clasa aleasa: <br /> <form name="servForm" method="POST"> <table border="0"> <?php while($plm = mysql_fetch_array($rezultat1)) : ?> <?php if ($a++ %5 == 0) :?> <tr> <?php endif; ?> <td align="center"> <input type="checkbox" name="checkbox2[]" value="<?php echo $plm['id']; ?>" /> </td> <td style="text-align:left"><?php echo $plm["materie"]; ?> </td> <?php if($a %5 == 0) : ?> </tr> <?php endif; ?> <?php endwhile; ?> </table> <br/> <input type="reset" value="Sterge" /> <input type="submit" value="Salveaza" name="savebtn" /> </form>
Search data from the table using javascript
I am currently working on an application based on JavaScript and PHP. In my app, I have a table and a textbox - when I enter data in the textbox, the table should show the particular value that I typed. My current implementation does not work as I am not sure how to complete the search component - can anyone help? <table name="tablecheck" class="DataTable" id="results" > <thead> <tr ><th> </th> <th> </th> <th><center><b>COURSE CODE</b></center></th> <th><center>COURSE NAME</center></th></tr> </thead> <?php if(isset($records)) : foreach ($records as $row) : ?> <tbody> <tr id="rowUpdate" class="TableHeaderFooter"> <td> <?php echo anchor('coursemaster_site/delete/'.$row->id, 'Delete',array('onClick'=>"return confirm('ARE YOU WANT TO DELETE..?')")); ?> </td> <td> <input type=checkbox name="editcustomer[] " id="editcustomer[]" value="<?php echo $row->id ?>" > </td> <td > <input class="inputwide span2 " type="text" onblur="upper(this)" name="course_code_<?php echo $row->id ?>" id=" course_code_<?php echo $row->id ?>" value="<?php echo $row->course_code ; ?> " > </td> <td> <input class="inputmedium span2" type="text" name="course_name_<?php echo $row->id ?>" id="course_name_<?php echo $row->id ?>" value="<?php echo $row->course_name ; ?>" > </td> </tr> </tbody> <?php endforeach ; ?> <?php endif ; ?> </table> <form action="#" method="get" onSubmit="return false;"> <label for="q">Search Here:</label><input type="text" size="30" name="q" id="q" value="" onKeyUp="doSearch();" /> </form> <script> function doSearch() { var rowContainsSearchTerm, fullname; var q = document.getElementById("q"); var v = q.value.toLowerCase(); var rows = document.getElementsByTagName("tr"); var searchTermMoreThanTwoCharsLong = v.length > 2; for ( var i = 1; i < rows.length; i++ ) { fullname = concatInputValues(rows[i].getElementsByTagName("input")); if (fullname) { rowContainsSearchTerm = fullname.indexOf(v) > -1; if (searchTermMoreThanTwoCharsLong && rowContainsSearchTerm) { rows[i].style.backgroundColor = "#00F"; } else { rows[i].style.backgroundColor = ""; } } } } function concatInputValues(inputs){ var inputContents = ""; for (var j = 0; j < inputs.length; j++) { inputContents = inputContents + inputs[j].value; } return inputContents; } </script>
Have you considered using JQuery DataTables? The plugin has a really nice table view as well as automatically enabled search textbox, and should fit in easily with your JavaScript/PHP solution. See example table below: