mySQL duplicate date column - PHP needs to be displayed once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Right, lets try something else to see what were getting in the array

Code: Select all

<select id="dcdate" name="dcdate"> 
                <optgroup label="Please Select Date"> 
                        <option name="dropdate" value="0">Please Select</option> 
                <?php 
                        $i=0; 
                        $dateQuery = "SELECT DISTINCT date FROM walkthroughs WHERE user='$authUser' ORDER BY date DESC"; 
                        $dateResult = mysql_query($dateQuery) or die(mysql_error()); 
                        $num = mysql_num_rows($dateResult); 
//added tempDate array
								$tempDate = array();
                        while ($i<$num) { 
                                $zDate = mysql_fetch_assoc($dateResult); 
                                $yDate = $zDate['date']; 
                                $xDate = date('M (Y)', strtotime($yDate));
								
								
								//yDate format = 0000-00-00
								
								//substr() below should now make a format of 0000-00 which is year and month
								
								//lets empty the tempYDate to ensure it is empty
								$tempYDate = '';
								$tempYDate = substr($yDate, 0, -3);
								
								
								//this is a test to see whats in the array
								print_r($tempDate);
								
								
								//the first if in_array() is NULL as we have not yet added anything to tempDate array
								if(in_array($tempYDate, $tempDate)){
								//do nothing
								}else{
								 
                                echo "<option name=\"dropdate\" value=\"$yDate\">".$xDate."</option>";
								};//end if
								
								//now we add to the tempDate array the tempYDate which formats like 0000-00
								$tempDate[] = $tempYDate;
                                $i++; 
                        } 
                ?> 
                </optgroup> 
        </select>
I just changed the array location as I placed it in the loop :idea:

let me know what you get.
drewrockshard
Forum Commoner
Posts: 37
Joined: Sat May 29, 2004 6:07 pm
Location: Dallas, Texas
Contact:

Post by drewrockshard »

Since it wouldn't output to the screen cause it was inside a drop down .. I had to pull it outside of the dropdown to the screen. However, here's the output:

Code: Select all

Array ( ) Nov (2007)Array ( ) Nov (2007)
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

mmm, this is getting weired :)

Code: Select all

<select id="dcdate" name="dcdate"> 
                <optgroup label="Please Select Date"> 
                        <option name="dropdate" value="0">Please Select</option> 
                <?php 
                        $i=0; 
                        $dateQuery = "SELECT DISTINCT date FROM walkthroughs WHERE user='$authUser' ORDER BY date DESC"; 
                        $dateResult = mysql_query($dateQuery) or die(mysql_error()); 
                        $num = mysql_num_rows($dateResult); 
//added tempDate array
								$tempDate = array();
                        while ($i<$num) { 
                                $zDate = mysql_fetch_assoc($dateResult); 
                                $yDate = $zDate['date']; 
                                $xDate = date('M (Y)', strtotime($yDate));
								
								
								//yDate format = 0000-00-00
								
								//substr() below should now make a format of 0000-00 which is year and month
								
								//lets empty the tempYDate to ensure it is empty
								$tempYDate = '';
								$tempYDate = substr($zDate['date'], 0, -3);
								
								
								//this is a test to see whats in the array
								//print_r($tempDate); = Array ( ) Nov (2007)Array ( ) Nov (2007)
								//this is strange because it has "Nov" and it should have "11", it should not have changed, because only xDate has been strtotime();
								
								
								//the first if in_array() is NULL as we have not yet added anything to tempDate array
								if(in_array($tempYDate, $tempDate)){
								//do nothing
								}else{
								 
                                echo "<option name=\"dropdate\" value=\"$yDate\">".$xDate."</option>";
								};//end if
								
								//now we add to the tempDate array the tempYDate which formats like 0000-00
								$tempDate[] = $tempYDate;
                                $i++; 
                        } 
                ?> 
                </optgroup> 
        </select>
drewrockshard
Forum Commoner
Posts: 37
Joined: Sat May 29, 2004 6:07 pm
Location: Dallas, Texas
Contact:

Post by drewrockshard »

Saweeeeeet.

It works. I'm going to change a few things up a big (add crap to the DB) to make sure it works for good. Literally I'm going to add a few more dates to make sure it keeps only one result, but I think this is great! I really have appriciated the help. Let me just check this real fast and I'll reply with the results.
drewrockshard
Forum Commoner
Posts: 37
Joined: Sat May 29, 2004 6:07 pm
Location: Dallas, Texas
Contact:

Post by drewrockshard »

Hey man,

Okay, I just have one request. Would there be anyway to make the value of each option to be yyyy-mm instead of yyyy-mm-dd?

Other than that, it's lookin good :)
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

oh, glad it's working :D

yes no problem.

try this

Code: Select all

<select id="dcdate" name="dcdate"> 
                <optgroup label="Please Select Date"> 
                        <option name="dropdate" value="0">Please Select</option> 
                <?php 
                        $i=0; 
                        $dateQuery = "SELECT DISTINCT date FROM walkthroughs WHERE user='$authUser' ORDER BY date DESC"; 
                        $dateResult = mysql_query($dateQuery) or die(mysql_error()); 
                        $num = mysql_num_rows($dateResult); 
//added tempDate array
								$tempDate = array();
                        while ($i<$num) { 
                                $zDate = mysql_fetch_assoc($dateResult); 
                                $yDate = $zDate['date']; 
                                $xDate = date('M (Y)', strtotime($yDate));
								
								
								//yDate format = 0000-00-00
								
								//substr() below should now make a format of 0000-00 which is year and month
								
								//lets empty the tempYDate to ensure it is empty
								$tempYDate = '';
								$tempYDate = substr($zDate['date'], 0, -3);
								
								
								//this is a test to see whats in the array
								//print_r($tempDate); = Array ( ) Nov (2007)Array ( ) Nov (2007)
								//this is strange because it has "Nov" and it should have "11", it should not have changed, because only xDate has been strtotime();
								
								
								//the first if in_array() is NULL as we have not yet added anything to tempDate array
								if(in_array($tempYDate, $tempDate)){
								//do nothing
								}else{
								 
                                echo "<option name=\"dropdate\" value=\"$tempYDate\">".$xDate."</option>";
								};//end if
								
								//now we add to the tempDate array the tempYDate which formats like 0000-00
								$tempDate[] = $tempYDate;
                                $i++; 
                        } 
                ?> 
                </optgroup> 
        </select>
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Code: Select all

<select id="dcdate" name="dcdate"> 
                <optgroup label="Please Select Date"> 
                        <option name="dropdate" value="0">Please Select</option> 
                <?php 
                        $i=0; 
                        $dateQuery = "SELECT DISTINCT date FROM walkthroughs WHERE user='$authUser' ORDER BY date DESC"; 
                        $dateResult = mysql_query($dateQuery) or die(mysql_error()); 
                        $num = mysql_num_rows($dateResult); 
//added tempDate array
								$tempDate = array();
                        while ($i<$num) { 
                                $zDate = mysql_fetch_assoc($dateResult); 
                                $yDate = $zDate['date']; 
                                $xDate = date('M (Y)', strtotime($yDate));
								
								
								//yDate format = 0000-00-00
								
								//substr() below should now make a format of 0000-00 which is year and month
								
								//lets empty the tempYDate to ensure it is empty
								$tempYDate = '';
								$tempYDate = substr($zDate['date'], 0, -3);
								
								
								//this is a test to see whats in the array
								//print_r($tempDate); = Array ( ) Nov (2007)Array ( ) Nov (2007)
								//this is strange because it has "Nov" and it should have "11", it should not have changed, because only xDate has been strtotime();
								
								
								//the first if in_array() is NULL as we have not yet added anything to tempDate array
								if(in_array($tempYDate, $tempDate)){
								//do nothing
								}else{
								 
                                echo "<option name=\"dropdate\" value=\"$yDate\">".$tempYDate."</option>";
								};//end if
								
								//now we add to the tempDate array the tempYDate which formats like 0000-00
								$tempDate[] = $tempYDate;
                                $i++; 
                        } 
                ?> 
                </optgroup> 
        </select>
drewrockshard
Forum Commoner
Posts: 37
Joined: Sat May 29, 2004 6:07 pm
Location: Dallas, Texas
Contact:

Post by drewrockshard »

Woopsie. Misunderstanding.

I like how it used to look with the Month and the year. That was for what was between the <select> tags. But theres a separate HTML attribute for <select> .. the part for value="". In the value attribute, I'd like to make it yyyy-dd.

So this is what it looks like now:

Code: Select all

<option name="dropdate" value="2007-11-28">November 2007</option>
And I'd like it to be:

Code: Select all

<option name="dropdate" value="2007-11">November 2007</option>
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Code: Select all

<select id="dcdate" name="dcdate"> 
                <optgroup label="Please Select Date"> 
                        <option name="dropdate" value="0">Please Select</option> 
                <?php 
                        $i=0; 
                        $dateQuery = "SELECT DISTINCT date FROM walkthroughs WHERE user='$authUser' ORDER BY date DESC"; 
                        $dateResult = mysql_query($dateQuery) or die(mysql_error()); 
                        $num = mysql_num_rows($dateResult); 
//added tempDate array
								$tempDate = array();
                        while ($i<$num) { 
                                $zDate = mysql_fetch_assoc($dateResult); 
                                $yDate = $zDate['date']; 
                                $xDate = date('M (Y)', strtotime($yDate));
								
								
								//yDate format = 0000-00-00
								
								//substr() below should now make a format of 0000-00 which is year and month
								
								//lets empty the tempYDate to ensure it is empty
								$tempYDate = '';
								$tempYDate = substr($zDate['date'], 0, -3);
								
								
								//this is a test to see whats in the array
								//print_r($tempDate); = Array ( ) Nov (2007)Array ( ) Nov (2007)
								//this is strange because it has "Nov" and it should have "11", it should not have changed, because only xDate has been strtotime();
								
								
								//the first if in_array() is NULL as we have not yet added anything to tempDate array
								if(in_array($tempYDate, $tempDate)){
								//do nothing
								}else{
								 
                                echo "<option name=\"dropdate\" value=\"$tempYDate\">".$xDate."</option>";
								};//end if
								
								//now we add to the tempDate array the tempYDate which formats like 0000-00
								$tempDate[] = $tempYDate;
                                $i++; 
                        } 
                ?> 
                </optgroup> 
        </select>
drewrockshard
Forum Commoner
Posts: 37
Joined: Sat May 29, 2004 6:07 pm
Location: Dallas, Texas
Contact:

Post by drewrockshard »

Awesome! Resolved :)

Taht was great!. 4 pages of forum topic, but we got it goin. What I think is awesome is the fact that forum topics go on and on and on - but we were able to knock this one out cause we were both online and kept replying .. I rarely see that.

I do wanna thank you, and if you have some sort of AIM or any IM system and wanna keep in touch or anything, just PM me. I do alot of side web design (im more of a code monkey / web design (CSS / XHTML) type guy - so I make things look good :P.

Again, thanks :)
Post Reply