Solved

How would I do this as an array?

Posted on 2014-10-07
7
86 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 57

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 57

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 110

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 110

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 57

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

732 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