We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4385
    • 372 Posts
    Hello, I am just can’t get my head wrapped around this problem. I have been staring at for awhile. I trying to rebuild some code that I lost due to a server error. I think this was the solution I had, but I can’t get it to work now. Maybe my approach is flawed, but I can’t seem to look at from another perspective. Maybe someone here with fresh eyes can see it the answer.

    I have short set of results coming from a DB, which I want to store in an array that I want to match the data with. If there is no match, then fill it with 0.

    The data result is this for example.

    count dayname
    2 2
    5 4
    4 5
    1 6
    1 1

    I am trying to match up the column dayname with my array of daynames. Mysql and PHP seem to differ on the number for the day of the week.

    <?php
    
    
    $output .=  "<div id=\"content\" style=\"font-size: 11px;\">";
    $output .=  "<p>" . $prettyFrom . " - " . $prettyTo . "</p>";
    
    $sql =  	"SELECT COUNT(*) AS entry_count, DAYOFWEEK(entry_date) AS dayname " .
    		"FROM LB_forms LEFT JOIN LB_entries ON LB_forms.form_id = LB_entries.entry_form " .
    		"WHERE LB_forms.form_id = 1 AND entry_date BETWEEN 20090921 AND 20090927 GROUP BY dayname";
    $v=0;
    $rs = $mydb->query($sql);
    $num_rows = mysql_num_rows($rs);
    $weekList = array(array('Monday',2),array('Tuesday',3),array('Wednesday',4),array('Thursday',5),array('Friday',6),array('Saturday',7),array('Sunday',1));
    if (mysql_num_rows($rs) > 0){
    for($i = 0;$i<count($weekList);$i++){
    mysql_data_seek($rs, $i-$v);
    	$formRow = mysql_fetch_row($rs);
    	$chartArray[$i][] = $weekList[$i][0]; // day of the week
    		if($weekList[$i][1] == $formRow[1] ){
    			$chartArray[$i][] = $formRow[0];  // number of entries
    			$maxHits[] = $formRow[0];  // track of entries
    		} else {
    			$chartArray[$i][] = 0;  // no entries
    			$v++;
    		}
    }
    for($i = 0;$i<count($weekList);$i++){
    	$output .=  "<div style=\"margin-top:6px; width: 300px; height: 20px; background-color: #92b7d3;\">";
    	$output .=  	"<div style=\"border-right: 1px solid white; width: " . $chartArray[$i][1]/max($maxHits)*90 ."%; height: 20px; background-color: #5b93bf;\"></div>";
    	$output .=  	"<div style=\"margin-top: -20px; color: white; padding-left: 4px;\"><b>" . $chartArray[$i][0] . "</b></div>";
    	$output .=  	"<div style=\"text-align: right; margin-top: -20px; color: white; padding-right: 4px;\">" . $chartArray[$i][1] . "</div>";
    	$output .=  "</div>";
    }
    } else {
    $output .=  "<p>no results found</p>";
    }
    $output .=  "</div>";
    return $output;
    ?>
    



    I would appreciate any help offered, my eyes are shot from staring at the screen to long.

      DropboxUploader -- Upload files to a Dropbox account.
      DIG -- Dynamic Image Generator
      gus -- Google URL Shortener
      makeQR -- Uses google chart api to make QR codes.
      MODxTweeter -- Update your twitter status on publish.
      • 3749
      • 24,544 Posts
      IIRC, MySQL thinks Sunday is day 0. Your array had Monday as day zero.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        My own eyes are bloodshot a bit too, but there’s gotta be an easier way.... here are a couple thoughts.

        I’m sure there’s a way to tweak PHP’s dates to make them match up how you want, but I suspect that’s the wrong way to go about this. Can you back up a bit? I can’t tell exactly what the end result is. Can you describe what you want to see without any code or queries?

        Have you looked at MySQL’s DAYNAME() function? It’ll return "Monday" instead of a number.
        http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

        Hopefully your columns are using actual date types and not integer representations. If you’re stuck with integers masquerading as dates, I would seriously consider restructuring your table, but you can use MySQL’s STR_TO_DATE function... something like this:
        DAYNAME(STR_TO_DATE( release_date, '%d%m%Y'))


        It’s probably a pain to force MySQL to return null values for dates in this case, simply because you may not have data for those days. But once you get the data organized you should be able to iterate over an array of days.

        Something like:
        SELECT 
        	DAYNAME(`entry_date`) as `day`
        	,COUNT(*) as `entry_count`
        FROM 
        	LB_forms LEFT JOIN LB_entries ON LB_forms.form_id = LB_entries.entry_form 
        WHERE
        	`entry_date` BETWEEN '20090921' AND '20090927'
        GROUP BY 
        	DAYNAME(`entry_date`)
        


        Exactly how you fetch that data and how it’s packaged depends on the exact mysql driver and function you use, but imagine something like this:

        $rs = $mydb->query($sql); // I'm assuming this returns an array of hashes
        $days_of_week = array('Sunday','Monday', ... );
        
        foreach ($days_of_week as $day) {
           foreach ($rs as $row) {
              if ($day == $row['day']) {
                 print "Day: $d had " . $row[$d] . " entries.";
              }
           }
        }



        Are you charting this? Have you looked at Google Chart? Did you post simplified code? Do you have your query in a dedicated function? I always shudder when I see SQL queries mixed with display code. Your query would be easier to read if you included table prefixes on all columns -- e.g. is entry_date LB_entries.entry_date or LB_forms.entry_date? entry_count?
          • 4385
          • 372 Posts
          Yes, I am trying to generate a simple chart of entries for last week. I have used Google charts before, but thought this was an easy chart of only seven days. I have a function that takes the current day and determines the date range for last week. It goes from Monday to Sunday. I had started with DAYNAME() first but then realized that my days were off. So I used DAYOFWEEK() to I could matchup with my loop. Comparing 2 = 2 for monday’s entries, and so on. I was trying to reset the mysql index to rollback on the query on the days that didn’t have any entries.


          SELECT COUNT(*),DAYOFWEEK(LB_entries.entry_date) AS dayname FROM LB_forms LEFT JOIN LB_entries
          ON LB_forms.form_id = LB_entries.entry_form WHERE LB_forms.form_id = 1 AND LB_entries.entry_date BETWEEN 20090921 AND 20090927235959 GROUP BY DATE(LB_entries.entry_date)
          
            DropboxUploader -- Upload files to a Dropbox account.
            DIG -- Dynamic Image Generator
            gus -- Google URL Shortener
            makeQR -- Uses google chart api to make QR codes.
            MODxTweeter -- Update your twitter status on publish.
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            Is this part correct?
            GROUP BY DATE(LB_entries.entry_date)


            I would think you want this:
            GROUP BY DAYOFWEEK(LB_entries.entry_date)


            Sorry, I’m still not following you... what do you mean by "determines the date range for last week"?

            Do you have things working now?
              • 4385
              • 372 Posts
              Thanks for help I appreciate all the help this forum provides.

              I came back to the problem with a fresh look. I went ahead and put the results of the query into an array. Then compared the arrays with the days of the week that I had and was able to pad it correctly with 0 for the days that were not in the database.
                DropboxUploader -- Upload files to a Dropbox account.
                DIG -- Dynamic Image Generator
                gus -- Google URL Shortener
                makeQR -- Uses google chart api to make QR codes.
                MODxTweeter -- Update your twitter status on publish.
                • 9207 ☆ A M B ☆
                • 2,475 Posts
                Good good good. Glad you got it working!