Hello i have the following script to generate a table
<?php error_reporting(null); ?>
<form action="" method="POST" autocomplete="off">
Row<br>
<input type="text" value="<?php echo $_POST['row'] ?>" name="row"><br/>
Kolom<br>
<input type="text" value="<?php echo $_POST['kolom'] ?>" name="kolom">
<input type="hidden" name="aksi" value="gen">
<br/><br/>
<input type="submit" value="Submit">
</form>
<?php
if($_POST['aksi']=="gen"){
echo "<table border=1>";
for ($i=1; $i<=$_POST['row']; $i++) { ?>
<tr>
<?PHP for ($y=1; $y<=$_POST['kolom']; $y++) { ?>
<td>Data</td>
<?php } ?>
</tr>
<?php }
echo "</table>";
}
?>
And i have a script to generate a fibonacci number
<?php
$first=0;
$second=1;
echo "$first $second";
for ($i=0; $i<10; $i++)
{
$third = $second + $first;
echo " $third";
$first = $second;
$second = $third;
}
?>
I want to join the 2 scripts.
when i a generate a table, Fibonacci needs to be placed inside the table, i don't know how to solve this.
This Result like this
This should do the trick. Cannot test so if there are any typos keep them :)
<?php error_reporting(null); ?>
<form action="" method="POST" autocomplete="off">
Row<br>
<input type="text" value="<?php echo $_POST['row'] ?>" name="row"><br/>
Kolom<br>
<input type="text" value="<?php echo $_POST['kolom'] ?>" name="kolom">
<input type="hidden" name="aksi" value="gen">
<br/><br/>
<input type="submit" value="Submit">
</form>
<?php
if($_POST['aksi']=="gen"){
$first=0;
$second=1;
?>
<table border=1>
<?php
for ($i=0; $i<$_POST['row']; $i++) {
?>
<tr>
<?PHP
for ($y=0; $y<$_POST['kolom']; $y++) {
/* first two should be 0 and 1*/
if($y < 2 && $i == 0) {
?>
<td>0</td>
<td>1</td>
<?php
$y += 2;
} else {
$third = $second + $first;
?>
<td><?php echo($third); ?></td>
<?php
$first = $second;
$second = $third;
}
}
?>
</tr>
<?php
}
}
?>
</table>
I'm very new to PHP. For a class assignment, we need to make a Tic Tac Toe game. So far, this is my code:
<html>
<body>
<h1>Tic Tac Toe</h1>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="input">
<input type="submit" name="submit">
</form>
<?php
//Display Determinations
//Top Left
if (!ISSET($_POST['submit'])) {
$GLOBALS['ul_truefalse'] = true;
$GLOBALS['turn'] = 1;
$this_happened = "!isset";
}
if (ISSET($_POST['submit'])) {
$GLOBALS['turn'] = $GLOBALS['turn'] + 1;
if ($GLOBALS['turn'] == 3) {
$GLOBALS['turn'] = 1;
}
}
if ($GLOBALS['ul_truefalse'] == true) {
$GLOBALS['ul_display'] = "UL";
if (ISSET($_POST['input']) and $_POST['input'] == "ul" and $GLOBALS['turn'] == 1) {
$GLOBALS['ul_display'] = "X";
$GLOBALS['ul_truefalse'] = false;
$this_happened = "p1 ul";
}
if (ISSET($_POST['input']) and $_POST['input'] == "ul" and $GLOBALS['turn'] == 2) {
$GLOBALS['ul_display'] = "O";
$GLOBALS['ul_truefalse'] = false;
}
}
echo "Player " . $GLOBALS['turn'] . ", it's your turn!";
echo $this_happened;
?>
<table border="1" width="40%">
<tr>
<td><?php echo $GLOBALS['ul_display'] ?></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>
Here is the output (left side is when the page is loaded, right side is when player 1 enters "ul"): http://i.imgur.com/G0JaXYY.png
The problem is when if (!ISSET($_POST['submit'])) { becomes false, the strings defined in that if statement lose their values, causing the undefined variable errors. I tried storing them in a hidden form box, but it didn't fix the issue. I sent my code to a person from a forum, and he said it worked. Why is this happening? How do I fix this?
Try this:
<html>
<body>
<h1>Tic Tac Toe</h1>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="input">
<input type="submit" name="submit">
</form>
<?php
if(!session_id())
session_start();
//Display Determinations
//Top Left
if (!ISSET($_POST['submit'])) {
$_SESSION['ul_truefalse'] = true;
$_SESSION['turn'] = 1;
$this_happened = "!isset";
}
if (ISSET($_POST['submit'])) {
$_SESSION['turn'] = $_SESSION['turn'] + 1;
if ($_SESSION['turn'] == 3) {
$_SESSION['turn'] = 1;
}
}
if ($_SESSION['ul_truefalse'] == true) {
$_SESSION['ul_display'] = "UL";
if (ISSET($_POST['input']) and $_POST['input'] == "ul" and $_SESSION['turn'] == 1) {
$_SESSION['ul_display'] = "X";
$_SESSION['ul_truefalse'] = false;
$this_happened = "p1 ul";
}
if (ISSET($_POST['input']) and $_POST['input'] == "ul" and $_SESSION['turn'] == 2) {
$_SESSION['ul_display'] = "O";
$_SESSION['ul_truefalse'] = false;
}
}
echo "Player " . $_SESSION['turn'] . ", it's your turn!";
echo $this_happened;
?>
<table border="1" width="40%">
<tr>
<td><?php echo $_SESSION['ul_display'] ?></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>
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.
I am trying to create a program that creates random registration keys and stores it in the database.If the user/customer already has a registration code it displays that key stored in the database in a text box on click 'else' it generates a new key and stores it in the database.The problem is i am not being able to store the key in the database.My code is:
<?php
if (isset($_POST['keygen'])){
$customer_no = $_POST['customer_no'];
$result = mysql_query("SELECT * FROM customer WHERE customer_no = '$customer_no'");
$row = mysql_fetch_array($result);
$keyString = $row['key'];
if($keyString == ""){
$keyString = generateRandomString();
$query = "UPDATE 'customer' SET key ='$keyString' WHERE customer_no = '$customer_no'";
mysql_query($query);
echo $keyString;
}
else{
echo $keyString;
}
}
function generateRandomString($length = 8) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
?>
this is my HTML:
<div id="content" class="box2">
<div class="login">
<form action="" method="post" style="margin:12px;">
<table class="nostyle">
<tr>
<td align="center">
<label style="font-size:16px;"><strong>Customer ID: </strong></label>
<select name="customer_no">
<?php $result_customer= mysql_query('SELECT customer_no FROM customer ORDER BY customer_no'); ?>
<?php while($row_customer= mysql_fetch_assoc($result_customer)) { ?>
<option <?php if ($row_customer['customer_no']=='') { ?> selected="selected"<?php } ?>> <?php echo htmlspecialchars($row_customer['customer_no']); ?> </option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td align="center"><label style="font-size:16px;"><br /><strong>Register Key: </strong></label>
<input type="text" id="key" class="input-text" name="key" size="20" align="middle" value = " <?=$row["key"];?>"></td>
</tr>
<td align="center"><br /><input type="submit" id="keygen" class="input-submit" name="keygen" value="Generate" onclick=""/>
</td>
</tr>
</table>
</form>
</div>
</div>
I am a newbie, and am not that sure about the code.Please help!
You first have to do mysqli_connect. Look at http://php.net. Also you should use mysqli_ functions, since mysql_ functions are deprecated for newer versions of php.
I have 35 records of statename in mysql table. I have divided the data into two columns
Here is my php code for the requirement
<td>
<table class="statetable">
<?
//******Fetching State Name Dynamically*****//
$fetchstate = mysql_query("SELECT LocationName FROM servicedesklocationmaster group by LocationName ASC");
$half1 = floor(mysql_num_rows($fetchstate)/2);
echo $half1;
$count = 0;
// First Half State
while($count <= $half1 && $row = mysql_fetch_array($fetchstate))
{
$statename = $row['LocationName'];
$count++;
?>
<tr>
<td>
<font size="1.5">
<input type="checkbox" name="Location[]" id="Location" checked value="<?php echo $statename;?>" onClick="CheckEachAsset()"><?php echo $statename;?><br>
</font>
</td>
<?
}
//echo $count;
?>
</tr>
</table>
</td>
<td>
<table class="statetable">
<?
// Second Half State
while($row = mysql_fetch_array($fetchstate))
{
$statename = $row['LocationName'];
?>
<tr>
<td>
<font size="1.5">
<input type="checkbox" name="Location[]" id="Location"
checked value="<?php echo $statename;?>" onClick="CheckEachAsset()"><?php echo $statename;?><br>
</font>
</td>
<?
}
?>
</tr>
</table>
</td>
Now as per my new requirement i want to divide this into 4 columns can anybody suggest me
how to achieve it
$quater = floor(mysql_num_rows($fetchstate)/4);
$count = 0;
while($row = mysql_fetch_array($fetchstate)) {
if($count < $quater) {
// echo table content1
}
if($count >= $quater && $count < $quater*2) {
// echo table content2
}
if($count >= $quater*2 && $count < $quater*3) {
// echo table content3
}
if($count >= $quater*3 && $count < $quater*4) {
// echo table content4
}
$count++;
}
<td>
<table class="statetable">
<?
$fetchstate = mysql_query("SELECT LocationName FROM servicedesklocationmaster group by LocationName ASC");
$quart= floor(mysql_num_rows($fetchstate)/4);
echo $quart;
$count = 0;
$times=0;
while($times<=4 && $row = mysql_fetch_array($fetchstate))
{
$statename = $row['LocationName'];
$count++;
$times++;
?>
<tr>
<td>
<font size="1.5">
<input type="checkbox" name="Location[]" id="Location" checked value="<?php echo $statename;?>" onClick="CheckEachAsset()"><?php echo $statename;?><br>
</font>
</td>
<?
if($count==$quart && $times!=4){
$count=0;
?>
</tr>
</table>
</td>
<td>
<table class="statetable">
<?
}
}
//echo $count;
?>
</tr>
</table>
</td>