Solved

How would I do this as an array?

Posted on 2014-10-07
7
85 Views
Last Modified: 2014-10-07
I've a pulldown that reveals the months of the year. I want only the months that are in my database so I wrote this:

					<option selected>Month</option>
					<?php 
					$cur_month="";
					$the_month="";
					$brice="select DISTINCT posted_day from verizon order by posted_day";
					$brice_query=mysqli_query($cxn, $brice)
					or die("Couldn't make Brice happen");
					while($brice_row=mysqli_fetch_assoc($brice_query))
					{
					extract($brice_row);
					$the_month = date("F", strtotime($brice_row['posted_day']));
						if($the_month=="January" AND $the_month<>$cur_month)
						{
						?>
						<option>January</option>
						<?php
						}
							elseif($the_month=="February" AND $the_month<>$cur_month)
							{
							?>
							<option>February</option>
							<?php
							}
							elseif($the_month=="March" AND $the_month<>$cur_month)
							{
							?>
							<option>March</option>
							<?php
							}
							elseif($the_month=="April" AND $the_month<>$cur_month)
							{
							?>
							<option>April</option>
							<?php
							}
							elseif($the_month=="May" AND $the_month<>$cur_month)
							{
							?>
							<option>May</option>
							<?php
							}
							elseif($the_month=="June" AND $the_month<>$cur_month)
							{
							?>
							<option>June</option>
							<?php
							}
							elseif($the_month=="July" AND $the_month<>$cur_month)
							{
							?>
							<option>July</option>
							<?php
							}
							elseif($the_month=="August" AND $the_month<>$cur_month)
							{
							?>
							<option>August</option>
							<?php
							}
							elseif($the_month=="September" AND $the_month<>$cur_month)
							{
							?>
							<option>September</option>
							<?php
							}
							elseif($the_month=="October" AND $the_month<>$cur_month)
							{
							?>
							<option>October</option>
							<?php
							}
							elseif($the_month=="November" AND $the_month<>$cur_month)
							{
							?>
							<option>November</option>
							<?php
							}
							elseif($the_month=="December" AND $the_month<>$cur_month)
							{
							?>
							<option>December</option>
							<?php
						}
						else
						{
						continue;
						}
					$cur_month=date("F", strtotime($brice_row['posted_day']));
					}
					?>
					<option>_________________________</option>
					</select>

Open in new window


It works fine, but I want to avoid having to loop through the table more than once. I've got a lot of records and I don't want to add an unnecessary burden to the user's experience by making them wait any longer than they would have to (I've got 200 million rows to consider).

I have the posted_day field indexed, so it doesn't take very long at all, but in the interest of keeping things as streamlined as possible, I want to store the results of the query that I've written above as an array and then, if I want to run the same conditional if statements for the day and the year, I can do that without having to do a select statement more than once.

Make sense?

In other words, can I store the months retrieved in the above query so if I had another pulldown that featured the months of the year, I could do all my ifs and elseifs against an array that has already been defined rather than having to loop through all my rows another time?

What do you think?
0
Comment
Question by:brucegust
  • 3
  • 2
  • 2
7 Comments
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 40366364
If you only want the ones in your database can you not just do the following

SELECT DISTINCT MONTH(posted_day) FROM verizon ORDER BY MONTH(posted_day)

Open in new window

That will give you rows of all the months that exist in your data? You can now loop through them and output the list
0
 
LVL 55

Assisted Solution

by:Julian Hansen
Julian Hansen earned 150 total points
ID: 40366379
Or even better
SELECT DISTINCT MONTHNAME(posted_day) FROM verizon ORDER BY MONTH(posted_day)

Open in new window

0
 

Author Comment

by:brucegust
ID: 40366400
Julian, I can smell what you're cooking, but how do I store the output in an array?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

Accepted Solution

by:
Ray Paseur earned 350 total points
ID: 40366462
After the SELECT query, you will have a results set.  You iterate over the results set like this:

$sql = "SELECT monthname... whatever to get THE RIGHT ORDER AND DISTINCT month names";
$res = mysqli_query($cxn, $sql);
if (!$res) trigger_error("FAIL: $sql", E_USER_ERROR);
$months = array();
while($row = mysqli_fetch_object($res))
{
    $months[] = $row->monthname;
}
var_dump($months);

Open in new window

0
 

Author Comment

by:brucegust
ID: 40366476
That's what I was looking for! Thanks!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40366503
Suggestion for going forward on this project... Create a test data set (obviously something less than 200MM rows) that is representative of your working data set.  Store it on your server in a .txt file or similar -- just so it is something small, but representative that we can download and use for testing.  Then when you have questions about how to construct the code you can show us the test data and we can give more complete (perhaps even tested) code examples.
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 40366521
  <option selected>Month</option>
<?php 
  // RETRIEV DISTINC MONTH NAMES IN MONTH ORDER
  $brice="SELECT DISTINCT MONTHNAME(posted_day) AS MonthName FROM verizon ORDER BY MONTH(posted_day)";
  $brice_query=mysqli_query($cxn, $brice) or die("Couldn't make Brice happen");
  // INITIALISE OUR ARRAY
  $months = array()
  // LOOP THROUGH RECORDS
  while($brice_row=mysqli_fetch_assoc($brice_query)) :
    // ADD MONTH NAME TO ARRAY
    $months[] = $brice_row['MonthName'];
    // OUTPUT THE OPTION
?>
  <option><?php echo $brice_row['MonthName'];?></option>
<?php
  endwhile;
?>
  <option>_________________________</option>
</select>

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question