Solved

How would I do this as an array?

Posted on 2014-10-07
7
88 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

690 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