Jump to content

Loops


Borderline

Recommended Posts

I am looking at producing some data, code below. I would like three content boxes on one page, with the WHERE statements looking like this;Content box 1: WHERE date='2010-12-01' AND ref='1'Content box 2: WHERE date='2010-12-01' AND ref='2'Content box 3: WHERE date='2010-12-01' AND ref='3'I understand that I could potentially use loops to repeat the code, and I wondered whether it would be possible to use loops in this instance. Any advice on the best way forward, and how to implement it would be greatly appreciated.

<div id="content">	<h2>			<?php		$result = mysql_query("SELECT * FROM racetitle WHERE date='2010-12-01' AND ref='1'")		or die(mysql_error());  		$row = mysql_fetch_array( $result );		echo "".$row['time'];		echo ": ".$row['race_name'];		?>	</h2>	<p>		<div id="racedetails">		<?php			$result = mysql_query("SELECT * FROM racetitle WHERE date='2010-12-01' AND ref='1'")			or die(mysql_error());  			$row = mysql_fetch_array( $result );			echo "<b><u>Race Analysis:<br></u></b> The ".$row['race_name'];			echo " is a class ".$row['raceclass'];			echo " ".$row['racetype'];			echo " ".$row['fencetype'];			echo " over ".$row['distance'];			echo " on ".$row['ground'];			echo " at  ".$row['track'];			echo " racecourse  ";		?>	</p></div>	<p>	<div id="previews">			<center>		<?php  			$data = mysql_query("SELECT * FROM preview WHERE date='2010-12-01' AND ref='1'") or die(mysql_error());  		?>				<center>		<table border='1' cellpadding='4' cellspacing='0' width='75%'>		<tr class='toprow'>			<th>No.</th> 			<th>Form</th> 			<th>Horse</th> 			<th>Weight</th> 			<th>Rating</th>			<th>Jockey</th> 			<th>Trainer</th>			<th>Trainer Comment</th>		</tr>		  		<?php		while($info = mysql_fetch_array($data)) {  				// links		$link='/features/previews/trainers.php';		$tctext='Add Trainer Comment';				?>		<tr> 			<td class='previewdata'><?php echo $info['cardno']; ?></td>			<td class='previewdata'><?php echo $info['form']; ?></td>			<td class='previewdata'><?php echo $info['horse']; ?></td>			<td class='previewdata'><?php echo $info['weight']; ?></td>			<td class='previewdata'><?php echo $info['rating']; ?></td>			<td class='previewdata'><?php echo $info['jockey']; ?></td>			<td class='previewdata'><?php echo $info['trainer']; ?></td>						<td class='previewdata'><a href="<?php echo $link; ?>"><?php echo $tctext;?></a></td>		</tr><tr>			<td colspan='8'><?php echo $info['comment']; ?></td>		</tr>			<?phpif (!empty($info['trainercomment']) ) { 		?>						<tr><td class='trainercomment' bgcolor='#FFFFB9' colspan='8'>Comments from <?php echo $info['trainer']; ?>:  			<?php echo $info['trainercomment']; ?></td>				<?php					}			else			{		?>						<tr><td class='trainercomment' colspan='8'>Comments from <?php echo $info['trainer']; ?>:  			<?php echo $info['trainercomment']; ?></td>		<?php } ?>		 </tr>		<?php		}  		?>		</table> 	</center></p></div>		<div id="tipping">Tipping Service:<br>		<?php						$query = "SELECT * FROM tips WHERE date='2010-12-01' AND ref='1'"; 	 			$result = mysql_query($query) or die(mysql_error());			while($row = mysql_fetch_array($result)){			echo $row['tipster']. " - ". $row['tip'];			echo "<br />";}		?>			<p>		<div id="verdict">Verdict:<br>		<?php						$query = "SELECT * FROM racetitle WHERE date='2010-12-01' AND ref='1'"; 	 			$result = mysql_query($query) or die(mysql_error());			while($row = mysql_fetch_array($result)){			echo $row['verdict'];			echo "<br />";}		?>	</p>			</div></div>

Link to comment
Share on other sites

is there a reason why you keep repeating the same line over and over throughout the page?

$query = mysql_query("SELECT * FROM racetitle WHERE date='2010-12-01' AND ref='1'");
there are couple of ways of doing this potentially. you could use a loop and and an array and store the information that way, incrementing the ref and array index value using the loop's iterator value. or, depending on what ref is (maybe its some sort of index of the table itself?) you could get the entire contents of the table and just limit the result to 3, in ascending order.
Link to comment
Share on other sites

well, first off, I think most developers usually perform all their calculations and executions at the top of the page, and then just pass around or use variables through out the page. in the case where you are repeating the same lines, just have the line execute once at the beginning of the page and then just execute the WHILE statements when you need it, no need to make multiple SQL queries when you've already got the data. Basically, try and condense all those calls and put them at the beginning of the page.Also, is ref going to be hard-coded into the page or does the number come from some sort of user input? If it's hardcoded, and you are going to want to get all three results in a loop, try something along these lines:

//create an array$results = array();for(var $i = 0; $i < 3; $i++){  //loop will run three times, and store the results in an index in the results array, for refs 1, 2, and 3  results[$i] = mysql_query("SELECT * FROM racetitle WHERE date='2010-12-01' AND ref=($i + 1)");};

now you can output the results from ref=1 by using $results[0], like so:

$row = mysql_fetch_array($results[0]);echo "".$row['time'];echo ": ".$row['race_name'];

ref=2 would be the same thing, except use $results[1].the thing you should know is that PHP runs before a webpage is rendered, so all these calculatations happen before the user sees anything. Hence, just run all your queries and store them in variables at the beginning of the document, and just insert the output code where needed. no need to run extra executions if you've already done it once.

Link to comment
Share on other sites

however, if you are getting the value of ref dynamically, then you need to access the value by either then incoming $_POST or $_GET array (depending on your implementation) and then just assign that to a variable instead and use that in the query.

<?php$ref = $_POST['ref']; $result = mysql_query("SELECT * FROM racetitle WHERE date='2010-12-01' AND ref=$ref")			 or die(mysql_error());  $row = mysql_fetch_array( $result );echo "".$row['time'];echo ": ".$row['race_name'];?>

Link to comment
Share on other sites

I've had a look into your suggestion (working at the moment on reducing the unnecessary code). I've removed most of the code in order to work through it bit by bit. I've only made a minor adjustment though, so I'm not sure I'm following your comments correctly. In the code below, I've removed the second WHERE line, which produces the result I'm after.

		<?php		$result = mysql_query("SELECT * FROM racetitle WHERE date='2010-08-30' AND ref='1'")		or die(mysql_error());  		$row = mysql_fetch_array( $result );		?>			<h2>			<?php 		echo "".$row['time'];		echo ": ".$row['race_name'];?>	</h2>	<p>		<div id="racedetails">			<b><u>Race Analysis:<br></b></u>			<?php			echo "The ".$row['race_name'];			echo " is a class ".$row['raceclass'];			echo " ".$row['racetype'];			echo " ".$row['fencetype'];			echo " over ".$row['distance'];			echo " on ".$row['ground'];			echo " at  ".$row['track'];			echo " racecourse  ";			?>	</p></div>

Is this what you mean, or is there a further way of cutting all this down in size?

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...