PHP - If I < 10 Then Add A Leading 0
hi,
I would like to add a leading 0 to my FOR loop for ($i=0; $i<23; $i++) if($i<10){ $i = 0$i; } { echo "<option value=".$i.">" . $i . "</option>"; } this gives and error: unexpected T_VARIABLE at the moment I have: 0 1 2 3 4 5 6 7 8 9 I would like: 00 01 02 03 04 05 06 07 08 09 Similar TutorialsLooking for the best method to conditionally strip leading zeros for the following situation: $a = array(01, 02, 03, ... 10, 11); $a < 10 ? $a = ? : ''; For the following result: 1, 2, 3, ... 10, 11 Is there a better method than explode? I have a db with entries as follows: A001 A002 A003 I would like to loop thru these and print them on screen. Instead of doing: echo A001 echo A002 echo A003 I'd like to find a better way since this database will scale up in the future and may get to A999. Is there a way to increment with leading placeholder zeros? for($x=0 ; $x < mysql_num_rows($myresult) ; $x++){ echo $row['A'.##increment here##']; } Thanks! Discovered something very strange with trim() and numbers with leading zeros. Code: [Select] <?php $number = 0456; echo trim($number); ?> That ouputs 302. How the heck does 0456 trimmed turn into 302? Is this a bug? Ok my question is, when they hit the "Play!" button, then I want the php code below to pop up. Like... i think I would use the GET method, but I'm not sure still. I don't think that really matters what method you use, but how do I say IF they click on the "Play" button, then lead them to this code so they can start playing the game. I'm entering this into the database, but I'm just trying to get this code first. I already know how to make it lead to the database and whatnot. <?php session_start(); include("logincheck.php"); ?> <?php include_once("header.php"); ?> <br> Welcome to the Six Dice game. <br><br><b>Instructions</b>: You play this game by randomly rolling a dice. If you land on 1-5, you earn 0 rp. If you land on a 6, you earn 500rp! The game is free so why not give it a shot? <br>You may only play this game 25 times a day! <br><br><form action="sixdice.php?roll" method="post"> <input type="submit" name="playsix" value="Play!" /> </form> <?php if(isset($_POST['playsix'])); $dice = rand(1,6); echo "You rolled a<br /><b>{$dice}</b>\n"; $winnings = "500"; if($dice == 6) { include("haha.php"); $cxn = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbdatabase); $username = $cxn->real_escape_string($_SESSION['username']); $sql = "UPDATE `Member` SET `rp` = rp+$winnings WHERE `username` = '$username'"; mysqli_query($cxn,$sql); }?> <FORM ACTION="sixdice.php" METHOD="post"> <INPUT TYPE="submit" VALUE="Roll Again!" /> </FORM> <?php include_once("footer.php"); ?> Hello, It's as subject says. Here is the code: <? //Config include_once "../../config/config.php"; //Variables $username = $_POST["username"]; $username = mysql_real_escape_string($username); $password = $_POST["password"]; $password = md5($password); //Check for User $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $query = mysql_query($query); //If user not found if(mysql_num_rows($query)!="1") { echo "No User Found"; } //If User Found else { while($row=mysql_fetch_array($query)) { $fname = $row["fname"]; $lname = $row["lname"]; $userid = $row["id"]; $admin = $row["admin"]; } setcookie("fname", "$fname"); setcookie("lname", "$lname"); setcookie("userid", "$userid"); setcookie("admin", "$admin"); header('Location: ../../../portal/index.php'); } ?> On the page it forwards to, I write print_r($_COOKIE); Yet it doesn't show my newly set cookies, yet the above snipped does on the script code above. George |