Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a database table with information inside. I am having an issue populating a foreach loop with the list of entries to the table. I am attempting to order the array by ascending date in variable[1] (event_date).

Eventually, the output will be a <div class="row-fluid"> for each of the entries and that each one will be in order of date and the ones behind the current date will not show up within the array.

Currently, I am getting an output that I am unsure as to what it really is. I have one entry, and I have 5 rows of events with odd letters in each.

In summary:

  1. Foreach loop of each table entry
  2. Array ordered by ascending date
  3. Dates previous to the current date do not show up in the array

My current code:

$result = mysql_query("SELECT event_id, event_date, event_time, name, body, image FROM events");

while($row = mysql_fetch_array($result, MYSQL_NUM)) {

            foreach($row as $variable) { ?>

                <div id="" class="row-fluid">
                    <div class="well span12">
                        <div class="span3">
                            <div class="thumbnail">
                                <a class="various fancybox.ajax" href="#"><img src="<?php echo $variable[5]; ?>"></a>
                            </div>
                        </div><!-- span3 -->
                        <div class="span1 ev-date text-center">
                                <span class="month"><?php echo $month; ?></span>
                                <span class="day"><?php echo $day; ?></span>
                                <span class="year"><?php echo $year; ?></span>
                        </div><!-- span1 -->
                        <div class="span8 ev-head">
                        <a class="various fancybox.ajax" href="#"><h3><?php echo $variable[3]; ?></h3></a>
                        <p><?php echo substr($variable[4], 0, 150); ?> <a class="various fancybox.ajax" href="#"><span class="readmore"> ..Read More</span></a></p>

                        </div><!-- span8 -->
                    </div><!-- well span12 -->
                </div><!-- appeal# -->  

        <?php
            }
        }
?>

You may also notice the <?php echo $month ?> $day and $year. Another question I have, although I am researching this as I type, is how can I strip the event_date variable into just DAY, MONTH, and YEAR? (I may as well ask whilst I am here) I am not sure if natcasesort is the correct function here.

Thanks!

share|improve this question
    
"while($row" is setting $row with one row of results. Delete your line with "foreach" and address your columns in this way: $row["event_id"] –  hellcode Jul 6 '14 at 17:55
    
If event_date is a date field you can use substr($row["event_date"],0,4) for $year, substr($row["event_date"],5,2) for $month and substr($row["event_date"],8,2) for $day –  hellcode Jul 6 '14 at 17:58
    
I've just re-read it all with a clear head. Removed the foreach loop and works currently! :) The next step is sorting it all using the event_date variable. Earliest date to latest, but ahead of the current date/time. So no previous events to be shown. –  Luke Cottingham Jul 6 '14 at 17:59
    
Then add "ORDER BY event_date DESC" to your SQL statement –  hellcode Jul 6 '14 at 18:03
    
That makes sense and works!! I'm self-teaching sql currently.. :) Thanks –  Luke Cottingham Jul 6 '14 at 18:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.