Jump to content

Recommended Posts

Hi Guys,

 

Im sure there is a way to simplify this into one query or something. Anyone have any ideas:

 

		<?php

		$mqry1 = "SELECT * FROM $blog WHERE month = 'January'";
		$mqry2 = "SELECT * FROM $blog WHERE month = 'February'";
		$mqry3 = "SELECT * FROM $blog WHERE month = 'March'";
		$mqry4 = "SELECT * FROM $blog WHERE month = 'April'";
		$mqry5 = "SELECT * FROM $blog WHERE month = 'May'";
		$mqry6 = "SELECT * FROM $blog WHERE month = 'June'";
		$mqry7 = "SELECT * FROM $blog WHERE month = 'July'";
		$mqry8 = "SELECT * FROM $blog WHERE month = 'August'";
		$mqry9 = "SELECT * FROM $blog WHERE month = 'September'";
		$mqry10 = "SELECT * FROM $blog WHERE month = 'October'";
		$mqry11 = "SELECT * FROM $blog WHERE month = 'November'";
		$mqry12 = "SELECT * FROM $blog WHERE month = 'December'";

		$mres1 = mysql_query($mqry1); $mres2 = mysql_query($mqry2); $mres3 = mysql_query($mqry3); $mres4 = mysql_query($mqry4); $mres5 = mysql_query($mqry5);
		$mres6 = mysql_query($mqry6); $mres7 = mysql_query($mqry7); $mres8 = mysql_query($mqry8); $mres9 = mysql_query($mqry9); $mres10 = mysql_query($mqry10);
		$mres11 = mysql_query($mqry11); $mres12 = mysql_query($mqry12);

		$cmres1 = mysql_num_rows($mres1); $cmres2 = mysql_num_rows($mres2); $cmres3 = mysql_num_rows($mres3); $cmres4 = mysql_num_rows($mres4); $cmres5 = mysql_num_rows($mres5);
		$cmres6 = mysql_num_rows($mres6); $cmres7 = mysql_num_rows($mres7); $cmres8 = mysql_num_rows($mres8); $cmres9 = mysql_num_rows($mres9); $cmres10 = mysql_num_rows($mres10);
		$cmres11 = mysql_num_rows($mres11); $cmres12 = mysql_num_rows($mres12);

		echo '<ul id="monthul">
			<li><a href="index.php?month=January">January</a>   ('.$cmres1.')</li>
			<li><a href="index.php?month=February">February</a>   ('.$cmres2.')</li>
			<li><a href="index.php?month=March">March</a>   ('.$cmres3.')</li>
			<li><a href="index.php?month=April">April</a>   ('.$cmres4.')</li>
			<li><a href="index.php?month=May">May</a>   ('.$cmres5.')</li>
			<li><a href="index.php?month=June">June</a>   ('.$cmres6.')</li>
			<li><a href="index.php?month=July">July</a>   ('.$cmres7.')</li>
			<li><a href="index.php?month=August">August</a>   ('.$cmres8.')</li>
			<li><a href="index.php?month=September">September</a>   ('.$cmres9.')</li>
			<li><a href="index.php?month=October">October</a>   ('.$cmres10.')</li>
			<li><a href="index.php?month=November">November</a>   ('.$cmres11.')</li>
			<li><a href="index.php?month=December">December</a>   ('.$cmres12.')</li>
		</ul>';

	?>

 

Any help would be appreciated.

 

Regards

Link to comment
https://forums.phpfreaks.com/topic/197468-simplify-a-code-snippet/
Share on other sites

Hi Guys,

 

Im sure there is a way to simplify this into one query or something. Anyone have any ideas:

 

		<?php

		$mqry1 = "SELECT * FROM $blog WHERE month = 'January'";
		$mqry2 = "SELECT * FROM $blog WHERE month = 'February'";
		$mqry3 = "SELECT * FROM $blog WHERE month = 'March'";
		$mqry4 = "SELECT * FROM $blog WHERE month = 'April'";
		$mqry5 = "SELECT * FROM $blog WHERE month = 'May'";
		$mqry6 = "SELECT * FROM $blog WHERE month = 'June'";
		$mqry7 = "SELECT * FROM $blog WHERE month = 'July'";
		$mqry8 = "SELECT * FROM $blog WHERE month = 'August'";
		$mqry9 = "SELECT * FROM $blog WHERE month = 'September'";
		$mqry10 = "SELECT * FROM $blog WHERE month = 'October'";
		$mqry11 = "SELECT * FROM $blog WHERE month = 'November'";
		$mqry12 = "SELECT * FROM $blog WHERE month = 'December'";

		$mres1 = mysql_query($mqry1); $mres2 = mysql_query($mqry2); $mres3 = mysql_query($mqry3); $mres4 = mysql_query($mqry4); $mres5 = mysql_query($mqry5);
		$mres6 = mysql_query($mqry6); $mres7 = mysql_query($mqry7); $mres8 = mysql_query($mqry8); $mres9 = mysql_query($mqry9); $mres10 = mysql_query($mqry10);
		$mres11 = mysql_query($mqry11); $mres12 = mysql_query($mqry12);

		$cmres1 = mysql_num_rows($mres1); $cmres2 = mysql_num_rows($mres2); $cmres3 = mysql_num_rows($mres3); $cmres4 = mysql_num_rows($mres4); $cmres5 = mysql_num_rows($mres5);
		$cmres6 = mysql_num_rows($mres6); $cmres7 = mysql_num_rows($mres7); $cmres8 = mysql_num_rows($mres8); $cmres9 = mysql_num_rows($mres9); $cmres10 = mysql_num_rows($mres10);
		$cmres11 = mysql_num_rows($mres11); $cmres12 = mysql_num_rows($mres12);

		echo '<ul id="monthul">
			<li><a href="index.php?month=January">January</a>   ('.$cmres1.')</li>
			<li><a href="index.php?month=February">February</a>   ('.$cmres2.')</li>
			<li><a href="index.php?month=March">March</a>   ('.$cmres3.')</li>
			<li><a href="index.php?month=April">April</a>   ('.$cmres4.')</li>
			<li><a href="index.php?month=May">May</a>   ('.$cmres5.')</li>
			<li><a href="index.php?month=June">June</a>   ('.$cmres6.')</li>
			<li><a href="index.php?month=July">July</a>   ('.$cmres7.')</li>
			<li><a href="index.php?month=August">August</a>   ('.$cmres8.')</li>
			<li><a href="index.php?month=September">September</a>   ('.$cmres9.')</li>
			<li><a href="index.php?month=October">October</a>   ('.$cmres10.')</li>
			<li><a href="index.php?month=November">November</a>   ('.$cmres11.')</li>
			<li><a href="index.php?month=December">December</a>   ('.$cmres12.')</li>
		</ul>';

	?>

 

Any help would be appreciated.

 

Regards

 

Tada!

 

$sql = mysql_query("SELECT * FROM $blog");
echo "<ul id='monthul'>";
while($months = mysql_fetch_assoc($sql)){
echo "<li><a href="index.php?month=$months[month]">$months[month]</a></li>";
}
echo "</ul>";

You should really not store month separately. I would use a DATE field or even a DATETIME field to store the data. This way you can easily order by datefield and it will come out in the proper order and you do not have to worry about modifying the display information. Then if you just want the month you use MySQL's DATE_FORMAT to format the datefield when it comes out of the database.

 

Just some food for thought.

Tada!

 

$sql = mysql_query("SELECT * FROM $blog");
echo "<ul id='monthul'>";
while($months = mysql_fetch_assoc($sql)){
echo "<li><a href="index.php?month=$months[month]">$months[month]</a></li>";
}
echo "</ul>";

 

That will only display the months that contain data. I need to display Jan through to Dec and numrically display how many entries that month has even if it is zero.

 

Thanks for that but any other ideas.

 

Kind regards

just a thought hardlt fully formed yet (psuedo code - someone smater than me could probably state it correctly)...

 

count as 1 where month = january

count as 2 where month = feb

etc etc

 

I suspect that this could be combined into one query

 

then

 

to display

 

(1) have an array of the name of the months

(2) loop thru the array displaying the links as with the corresponding values gatherd in the query

 

A. As there is no need for anything else fromm the db at this stage, it inscreases speed adn reduces server usage

B. simplifies the code

 

Make sense?

My take on this problem:

<?php
		$January = 0;
		$February = 0;
		$March = 0;
		$April = 0;
		$May = 0;
		$June = 0;
		$July = 0;
		$August = 0;
		$September = 0;
		$October = 0;
		$November = 0;
		$December = 0;

		$sql = "SELECT * FROM `$blog`";

		$result = mysql_query($sql);
		if(mysql_num_rows($result) > 0) {
			while($r = mysql_fetch_assoc($result)) {
				@$$r['month'] += 1;
			}
		echo '<ul id="monthul">
			<li><a href="index.php?month=January">January</a>   ('.$January.')</li>
			<li><a href="index.php?month=February">February</a>   ('.$February.')</li>
			<li><a href="index.php?month=March">March</a>   ('.$March.')</li>
			<li><a href="index.php?month=April">April</a>   ('.$April.')</li>
			<li><a href="index.php?month=May">May</a>   ('.$May.')</li>
			<li><a href="index.php?month=June">June</a>   ('.$June.')</li>
			<li><a href="index.php?month=July">July</a>   ('.$July.')</li>
			<li><a href="index.php?month=August">August</a>   ('.$August.')</li>
			<li><a href="index.php?month=September">September</a>   ('.$September.')</li>
			<li><a href="index.php?month=October">October</a>   ('.$October.')</li>
			<li><a href="index.php?month=November">November</a>   ('.$November.')</li>
			<li><a href="index.php?month=December">December</a>   ('.$December.')</li>
		</ul>';

	?>

My take on this problem:

<?php
		$January = 0;
		$February = 0;
		$March = 0;
		$April = 0;
		$May = 0;
		$June = 0;
		$July = 0;
		$August = 0;
		$September = 0;
		$October = 0;
		$November = 0;
		$December = 0;

		$sql = "SELECT * FROM `$blog`";

		$result = mysql_query($sql);
		if(mysql_num_rows($result) > 0) {
			while($r = mysql_fetch_assoc($result)) {
				@$$r['month'] += 1;
			}
		echo '<ul id="monthul">
			<li><a href="index.php?month=January">January</a>   ('.$January.')</li>
			<li><a href="index.php?month=February">February</a>   ('.$February.')</li>
			<li><a href="index.php?month=March">March</a>   ('.$March.')</li>
			<li><a href="index.php?month=April">April</a>   ('.$April.')</li>
			<li><a href="index.php?month=May">May</a>   ('.$May.')</li>
			<li><a href="index.php?month=June">June</a>   ('.$June.')</li>
			<li><a href="index.php?month=July">July</a>   ('.$July.')</li>
			<li><a href="index.php?month=August">August</a>   ('.$August.')</li>
			<li><a href="index.php?month=September">September</a>   ('.$September.')</li>
			<li><a href="index.php?month=October">October</a>   ('.$October.')</li>
			<li><a href="index.php?month=November">November</a>   ('.$November.')</li>
			<li><a href="index.php?month=December">December</a>   ('.$December.')</li>
		</ul>';

	?>

 

Jcbones you are a star !! Works a treat thank you very much !!

 

Out of curiosity what is the extra @$ for before @$$r['month']

 

Many thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.