Jump to content

[SOLVED] Leading Zero?


twilitegxa

Recommended Posts

I have the following code:

 

<?php

session_start();

//Access Tracking Snippet

//set up static variables
$page_title = "listcharacters.php";
$user_agent = getenv("HTTP_USER_AGENT");
$date_accessed = date("Y-m-d");

//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
$db = mysql_select_db("smrpg", $conn) or die(mysql_error());

//create and issue query
$sql = "insert into access_tracker values
('', '$page_title', '$user_agent', '$date_accessed')";
mysql_query($sql,$conn);
?>

<?php
//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
mysql_select_db("smrpg", $conn) or die(mysql_error());

//gather the scouts
$get_topics = "select id, identity,
date_format(create_time, '%b %e %Y at %r') as fmt_scout_create_time,
username, available from scouts where available = '0' order by create_time desc";
$get_topics_res = mysql_query($get_topics, $conn) or die(mysql_error());
if (mysql_num_rows($get_topics_res) < 1) {
//there are no topics, so say so
$display_block = "<p><em>No Scouts exist.</em></p>";
} else {
//get number of players
$get_num_posts = "select count(id) from scouts";
	$get_num_posts_res = mysql_query($get_num_posts, $conn)
		or die(mysql_error());
	$num_posts = mysql_result($get_num_posts_res, 0, 'count(id)');

//create the display string
$display_block = "
<table cellpadding=4 cellspacing=2 border=1>
<tr>
<th>SCOUTS</th>
<th># OF PLAYERS: $num_posts</th>
</tr>";

while ($topic_info = mysql_fetch_array($get_topics_res)) {
	$topic_id = $topic_info['id'];
	$topic_title = ucwords(strtolower($topic_info['identity']));
	$topic_create_time = $topic_info['fmt_scout_create_time'];
	$topic_owner = $topic_info['username'];

	//add to display
	$display_block .= "
	<tr>
	<td><a href=\"showprofile_scouts.php?id=$topic_id\">
		<strong>$topic_title</strong></a><br>
		<em>Created on $topic_create_time by</em> <strong>$topic_owner</strong></td>
		<td align=center> </td>
		</tr></table>";
	}
}

//gather the knights
$get_knights = "select id, identity,
date_format(create_time, '%b %e %Y at %r') as fmt_knight_create_time,
username, available from knights where available = '0' order by create_time desc";
$get_knights_res = mysql_query($get_knights, $conn) or die(mysql_error());
if (mysql_num_rows($get_knights_res) < 1) {
//there are no knights, so say so
$display_block .= "<p><em>No Knights exist.</em></p>";
} else {
//get number of players
$get_num_knights = "select count(id) from knights";
	$get_num_knights_res = mysql_query($get_num_knights, $conn)
		or die(mysql_error());
	$num_knights= mysql_result($get_num_knights_res, 0, 'count(id)');

//create the display string
$display_block .= "<table cellpadding=3 cellspacing=3 border=1>
<tr>
<th>KNIGHTS</th>
<th># OF PLAYERS: $num_knights</th>
</tr>";

while ($knight_info = mysql_fetch_array($get_knights_res)) {
	$knight_id = $knight_info['id'];
	$knight_identity = ucwords(strtolower($knight_info['identity']));
	$knight_create_time = $knight_info['fmt_knight_create_time'];
	$knight_username = $knight_info['username'];

	//add to display
	$display_block .= "
	<tr>
	<td><a href=\"showprofile_knights.php?id=$knight_id\">
		<strong>$knight_identity</strong></a><br>
		<em>Created on $knight_create_time by</em> <strong>$knight_username</strong></td>
		<td align=center> </td>
		</tr></table>";
	}
}

//gather the fdark_warriors
$get_fdark_warriors = "select id, identity,
date_format(create_time, '%b %e %Y at %r') as fmt_fdark_warrior_create_time,
username, available from fdark_warrior where available = '0' order by create_time desc";
$get_fdark_warriors_res = mysql_query($get_fdark_warriors, $conn) or die(mysql_error());
if (mysql_num_rows($get_fdark_warriors_res) < 1) {
//there are no female dark warriors, so say so
$display_block .= "<p><em>No Female Dark Warriors exist.</em></p>";
} else {
//get number of players
$get_num_fdark_warriors = "select count(id) from fdark_warrior";
	$get_num_fdark_warriors_res = mysql_query($get_num_fdark_warriors, $conn)
		or die(mysql_error());
	$num_fdark_warriors = mysql_result($get_num_fdark_warriors_res, 0, 'count(id)');

//create the display string
$display_block .= "<table cellpadding=3 cellspacing=3 border=1>
<tr>
<th>FEMALE DARK WARRIORS</th>
<th># OF PLAYERS: $num_fdark_warriors</th>
</tr>";

while ($fdark_warrior_info = mysql_fetch_array($get_fdark_warriors_res)) {
	$fdark_warrior_id= $fdark_warrior_info['id'];
	$fdark_warrior_identity = ucwords(strtolower($fdark_warrior_info['identity']));
	$fdark_warrior_create_time = $fdark_warrior_info['fmt_fdark_warrior_create_time'];
	$fdark_warrior_username = $fdark_warrior_info['username'];

	//add to display
	$display_block .= "
	<tr>
	<td><a href=\"showprofile_fdark_warrior.php?id=$fdark_warrior_id\">
		<strong>$fdark_warrior_identity</strong></a><br>
		<em>Created on $fdark_warrior_create_time by</em> <strong>$fdark_warrior_username</strong></td>
		<td align=center> </td>
		</tr></table>";
	}
}	

//gather the male dark_warriors
$get_mdark_warriors = "select id, identity,
date_format(create_time, '%b %e %Y at %r') as fmt_mdark_warrior_create_time,
username, available from mdark_warrior where available = '0' order by create_time desc";
$get_mdark_warriors_res = mysql_query($get_mdark_warriors, $conn) or die(mysql_error());
if (mysql_num_rows($get_mdark_warriors_res) < 1) {
//there are no male dark warriors, so say so
$display_block .= "<p><em>No Male Dark Warriors exist.</em></p>";
} else {
//get number of players
$get_num_mdark_warriors = "select count(id) from mdark_warrior";
	$get_num_mdark_warriors_res = mysql_query($get_num_mdark_warriors, $conn)
		or die(mysql_error());
	$num_mdark_warriors = mysql_result($get_num_mdark_warriors_res, 0, 'count(id)');

//create the display string
$display_block .= "<table cellpadding=3 cellspacing=3 border=1>
<tr>
<th>MALE DARK WARRIORS</th>
<th># OF PLAYERS: $num_mdark_warriors</th>
</tr>";

while ($mdark_warrior_info = mysql_fetch_array($get_mdark_warriors_res)) {
	$mdark_warrior_id= $mdark_warrior_info['id'];
	$mdark_warrior_identity = ucwords(strtolower($mdark_warrior_info['identity']));
	$mdark_warrior_create_time = $mdark_warrior_info['fmt_mdark_warrior_create_time'];
	$mdark_warrior_username = $mdark_warrior_info['username'];

	//add to display
	$display_block .= "
	<tr>
	<td><a href=\"showprofile_mdark_warrior.php?id=$mdark_warrior_id\">
		<strong>$mdark_warrior_identity</strong></a><br>
		<em>Created on $mdark_warrior_create_time by</em> <strong>$mdark_warrior_username</strong></td>
		<td align=center> </td>
		</tr>";
	}
}	

	//close up the table
	$display_block .= "</table>";
?>
<html>
<head>
<title>Sailor Moon RPG - Characters</title>
<style type="text/css" media="screen">
/*<![CDATA[*/
@import url(global.css); 
/*]]>*/
</style>
</head>
<body>
<!-- HEADER -->
<h1 class="logo">Sailor Moon RPG</h1>
<!-- /HEADER -->
<?php include("topnav.php"); ?>
<div id="main">
<?php include("includes/log.php"); ?>
<?php include("mainnav.php"); ?>
<h1>Characters In Sailor Moon RPG</h1>
<h3>Would you like to <a href="creationform.php">create a character</a>?</h3>
<?php print $display_block; ?>
</div>
<?php include("bottomnav.php"); ?><!-- FOOTER -->
<!-- FOOTER -->
<div id="footer_wrapper">
<div id="footer">
<p>Sailor Moon and all characters
are<br /> 
trademarks of Naoko Takeuchi.</p>
<p>Copyright © 2009 Liz Kula. All rights reserved.<br />
A product of <a href="#" target="_blank">Web Designs By Liz</a> systems.</p>
<div id="foot-nav">
<ul>
<li><a href="http://validator.w3.org/check?uri=http://webdesignsbyliz.com/digital/index.php" target="_blank"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></li>
<li><a href="http://jigsaw.w3.org/css-validator/validator?uri=http://webdesignsbyliz.com/digital/global.css" target="_blank"><img class="c2" src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" /></a></li>
</ul>
</div>
</div>
</div>
<!-- /FOOTER -->
</body>
</html>

 

I wanted to add a leading zero if the $num_knights is less than 10, but I can't get the code to work. Here is what I tried:

 

//create the display string
$display_block .= "<table cellpadding=3 cellspacing=3 border=1>
<tr>
<th>KNIGHTS</th>";
if ($num_knights<10){

         $num_knights = "0"+$num_knights;

      }
display_block .= "<th># OF PLAYERS: $num_knights</th>
</tr>";

 

But that didn't work. How do I do this?

Link to comment
https://forums.phpfreaks.com/topic/170196-solved-leading-zero/
Share on other sites

JavaScript uses the "+" format for concatenating strings. PHP uses the "." format.

 

<?php
//create the display string
   $display_block .= "<table cellpadding=3 cellspacing=3 border=1>
   <tr>
   <th>KNIGHTS</th>";
if ($num_knights<10){

         $num_knights = "0".$num_knights;

      }
   display_block .= "<th># OF PLAYERS: $num_knights</th>
   </tr>";
?>   

Link to comment
https://forums.phpfreaks.com/topic/170196-solved-leading-zero/#findComment-897808
Share on other sites

I'm getting:

 

Parse error: parse error in C:\wamp\www\listcharacters.php on line 93

 

Here's what I changed:

 

$display_block .= "<table cellpadding=3 cellspacing=3 border=1>
<tr>
<th>KNIGHTS</th>";
if ($num_knights<10){

         $num_knights = "0".$num_knights;

      }
  display_block .= "
<th># OF PLAYERS: $num_knights</th>
</tr>";

 

What am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/170196-solved-leading-zero/#findComment-897821
Share on other sites

Could be your missing a $ on line10

<php
$display_block .= "<table cellpadding=3 cellspacing=3 border=1>
   <tr>
   <th>KNIGHTS</th>";
   if ($num_knights<10){

         $num_knights = "0".$num_knights;

      }
     $display_block .= "
   <th># OF PLAYERS: $num_knights</th>
   </tr>";
?>

 

this works for me

<?php
$num_knights = 9;
$display_block .= "<table cellpadding=3 cellspacing=3 border=1>
   <tr>
   <th>KNIGHTS</th>";
   if ($num_knights<10){

         $num_knights = "0".$num_knights;

      }
     $display_block .= "
   <th># OF PLAYERS: $num_knights</th>
   </tr></table>";
  echo $display_block;
?>

Link to comment
https://forums.phpfreaks.com/topic/170196-solved-leading-zero/#findComment-897824
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.