is there a better way

This is a discussion on "is there a better way" within the PHP Forum section. This forum, and the thread "is there a better way are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jan 28th, 2008, 17:52
saltedm8's Avatar
Lead Administrator

SuperMember
Join Date: Nov 2005
Location: Always About
Age: 27
Posts: 1,298
Blog Entries: 1
Thanks: 1
Thanked 6 Times in 6 Posts
is there a better way

is there a way i can 'clean this up' i think it looks 'over the top' - sorry, uurrmm - what i mean it i wanted to shorten it and make it look a little more professional

thanks

PHP: Select all

<?php

    
//get this month
    
$month date("n");

    
$jump = isset($_POST['jump']) ? $_POST['jump'] : (isset($_GET['jump']) ? $_GET['jump'] : $month);

    require(
'main.php');

    
$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = 1 ") or die (mysql_error() . 'dying hurts');
    
$num mysql_fetch_assoc($res);
    
$num1 $num['num'];

    
$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = 2 ") or die (mysql_error() . 'dying hurts');
    
$num mysql_fetch_assoc($res);
    
$num2 $num['num'];

    
$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = 3 ") or die (mysql_error() . 'dying hurts');
    
$num mysql_fetch_assoc($res);
    
$num3 $num['num'];

    
$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = 4 ") or die (mysql_error() . 'dying hurts');
    
$num mysql_fetch_assoc($res);
    
$num4 $num['num'];

    
$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = 5 ") or die (mysql_error() . 'dying hurts');
    
$num mysql_fetch_assoc($res);
    
$num5 $num['num'];

    
$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = 6 ") or die (mysql_error() . 'dying hurts');
    
$num mysql_fetch_assoc($res);
    
$num6 $num['num'];

    
$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = 7 ") or die (mysql_error() . 'dying hurts');
    
$num mysql_fetch_assoc($res);
    
$num7 $num['num'];

    
$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = 8 ") or die (mysql_error() . 'dying hurts');
    
$num mysql_fetch_assoc($res);
    
$num8 $num['num'];

    
$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = 9 ") or die (mysql_error() . 'dying hurts');
    
$num mysql_fetch_assoc($res);
    
$num9 $num['num'];

    
$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = 10 ") or die (mysql_error() . 'dying hurts');
    
$num mysql_fetch_assoc($res);
    
$num10 $num['num'];

    
$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = 11 ") or die (mysql_error() . 'dying hurts');
    
$num mysql_fetch_assoc($res);
    
$num11 $num['num'];

    
$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = 12 ") or die (mysql_error() . 'dying hurts');
    
$num mysql_fetch_assoc($res);
    
$num12 $num['num'];

    
?>

       <?php

      $result
mysql_query("SELECT * FROM form WHERE MONTH(FROM_UNIXTIME(date)) = '".$jump."' ");
      
$option"date";


      
?>


      <form method= "POST">

      <!--

      <select name="ddate" id="ddate">
      <?php echo $option ;?>.
      </select>

      -->


      <select name="jump" id="jump"  onchange="location.href=this.options[this.selectedIndex].value">
      <option value="index.php?jump=1"<?php  echo $jump=='1'?' selected':''?> >January 2008<?php echo '&nbsp;&nbsp;('.$num1.')'?></option>
      <option value="index.php?jump=2"<?php  echo $jump=='2'?' selected':''?> >Febuary 2008<?php echo '&nbsp;&nbsp;('.$num2.')'?></option>
      <option value="index.php?jump=3"<?php  echo $jump=='3'?' selected':''?> >March 2008<?php echo '&nbsp;&nbsp;('.$num3.')'?></option>
      <option value="index.php?jump=4"<?php  echo $jump=='4'?' selected':''?> >April 2008<?php echo '&nbsp;&nbsp;('.$num4.')'?></option>
      <option value="index.php?jump=5"<?php  echo $jump=='5'?' selected':''?> >May 2008<?php echo '&nbsp;&nbsp;('.$num5.')'?></option>
      <option value="index.php?jump=6"<?php  echo $jump=='6'?' selected':''?> >June 2008<?php echo '&nbsp;&nbsp;('.$num6.')'?></option>
      <option value="index.php?jump=7"<?php  echo $jump=='7'?' selected':''?> >July 2008<?php echo '&nbsp;&nbsp;('.$num7.')'?></option>
      <option value="index.php?jump=8"<?php  echo $jump=='8'?' selected':''?> >August 2008<?php echo '&nbsp;&nbsp;('.$num8.')'?></option>
      <option value="index.php?jump=9"<?php  echo $jump=='9'?' selected':''?> >September 2008<?php echo '&nbsp;&nbsp;('.$num9.')'?></option>
      <option value="index.php?jump=10"<?php echo $jump=='10'?' selected':''?> >October 2008<?php echo '&nbsp;&nbsp;('.$num10.')'?></option>
      <option value="index.php?jump=11"<?php echo $jump=='11'?' selected':''?> >November 2008<?php echo '&nbsp;&nbsp;('.$num11.')'?></option>
      <option value="index.php?jump=12"<?php echo $jump=='12'?' selected':''?> >December 2008<?php echo '&nbsp;&nbsp;('.$num12.')'?></option>

      </select>


      </form><?php mysql_close($conn); ?>
__________________
My Recipe forum...don't click here
Last Blog Entry: Basic Advice for newbies (Feb 1st, 2008)
Reply With Quote

  #2 (permalink)  
Old Jan 28th, 2008, 18:17
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: is there a better way

This should slim it down for you.. (not tested but will work -- unless sytax error is found )

PHP: Select all

]<?php


    
//get this month
    
$month date("n");

    
$year date("Y");
    
    
// List of month names to write to form later... start array from 1 so that it will work in the loop
    
$monthNames(1=>'January''February''March''April''May''June''July''August''September''October''November''December');

    
$jump = isset($_POST['jump']) ? $_POST['jump'] : (isset($_GET['jump']) ? $_GET['jump'] : $month);

    require(
'main.php');
    
    
    
$nums = array(); // creat an array to fill with the numbers
    
    
$queryString 'SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = %d '// The query to add the num to
    
    // Loop through 12 times adding the result to each query
    
for ($i=1$i<13$i++)
    {
        
$res mysql_query(sprintf($queryString$i)) or die(mysql_error() . 'dying really hurts');
        
$num mysql_fetch_assoc($res);
        
$num[$i] = $num['num'];
    }

      
$resultmysql_query("SELECT * FROM form WHERE MONTH(FROM_UNIXTIME(date)) = '".$jump."' ");
      
$option"date";

        

      
?>


      <form method= "POST">

      <!--

      <select name="ddate" id="ddate">
      <?php echo $option ;?>.
      </select>

      -->
            <select name="jump" id="jump"  onchange="location.href=this.options[this.selectedIndex].value">
      <?php
      
      
for ($i=1$i<13$i++)
          echo 
'<option value="index.php?jump='$i'" ', ($jump==$i 'selected="selected"' ''), '>'$monthNames[$i], ' ',  $year'&nbsp;&nbsp;('$nums[$i], ')</option>';
        
        
?>

      </select>


      </form><?php mysql_close($conn); ?>
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Jan 28th, 2008, 18:35
saltedm8's Avatar
Lead Administrator

SuperMember
Join Date: Nov 2005
Location: Always About
Age: 27
Posts: 1,298
Blog Entries: 1
Thanks: 1
Thanked 6 Times in 6 Posts
Re: is there a better way

nice try, but

Parse error: parse error, unexpected T_STRING in C:\Program Files\EasyPHP 2.0b1\www\blog\search.php on line 8

line 8

PHP: Select all

 $month date("n"); 

__________________
My Recipe forum...don't click here
Last Blog Entry: Basic Advice for newbies (Feb 1st, 2008)
Reply With Quote
  #4 (permalink)  
Old Jan 28th, 2008, 20:32
saltedm8's Avatar
Lead Administrator

SuperMember
Join Date: Nov 2005
Location: Always About
Age: 27
Posts: 1,298
Blog Entries: 1
Thanks: 1
Thanked 6 Times in 6 Posts
Re: is there a better way

a friend of mine has done this,

PHP: Select all

<?php

    
//get this month and year
    
$month date("n");
    
$year date("Y");
    
    
$jump = isset($_POST['jump']) ? $_POST['jump'] : (isset($_GET['jump']) ? $_GET['jump'] : $month);

    require(
'main.php');
    
    
$month_tots = array();
    for (
$i=1$i<=12$i++) $month_tots[$i] = 0;       // ensure total for every month

    
$res mysql_query ("SELECT MONTH(FROM_UNIXTIME(date)) as mth, COUNT(*) AS num 
                        FROM form
                        WHERE YEAR(FROM_UNIXTIME(date)) = '$year'
                        GROUP BY mth"
);
     while (list(
$mth$num) = mysql_fetch_row($res)) 
     {
         
$month_tots[$mth] = $num;         // put month totals in the array
     
}
     
     
/**
     * create the dropdown
     */
     
echo '<form method="POST">
     <select name="jump" id="jump"  onchange="location.href=this.options[this.selectedIndex].value">'
;
     foreach (
$month_tots as $m => $num)
     {
         
$sel $jump==$m 'selected':'';
         
$dt date ('F Y'mktime(0,0,0,$m1$year));
         echo 
"<option value='index.php?jump=$m' $sel>$dt ($num)</option>";
     }
     echo 
'</select></form>'?>
     
    <?php mysql_close($conn); ?>
but i need to separate the pagementation from the drop down box ( totally seperate so i can 'include' each where i want in my site, at the moment the pagementation is directly above the jump menu

PHP: Select all

<?php include 'config.php';

global 
$jump;

$conn mysql_connect($dbhost$dbuser$dbpass) or die ('Error connecting to mysql');
mysql_select_db$dbname$conn );

$perPage 5// number to display on per page

// Start by counting the number of posts

$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = '".$jump."' ") or die (mysql_error() . 'dying hurts');

$num mysql_fetch_assoc($res);
$num $num['num'];

mysql_free_result($res);

// now work out how many pages there are, check if page has been sent via GET

$numPages ceil($num $perPage);

// Check to make sure pagenum will be 1 or greater, less than numpages and an actual number
$pageNum  = (!empty($_GET['id']) &&
             
is_numeric($_GET['id']) &&
             
$_GET['id'] > 1)

             ? (
$_GET['id'] < $numPages ?  ($_GET['id']) : $numPages) : 1// Set to 1 if not set or something dodgy

// Okay, now we run the query and set the limits LIMIT xx, yy (x= start from , y= limit)


$sql "SELECT * FROM form WHERE MONTH(FROM_UNIXTIME(date)) = '".$jump."' ORDER BY date DESC LIMIT " . ($pageNum 1) * $perPage ", $perPage";
$res mysql_query($sql$conn) or die(mysql_error());

while (
$row mysql_fetch_object($res))
{
echo 
'<hr></hr>';
      print  
$row->title ;
      echo 
nl2br("\n");
      print 
strftime('%d %B %Y'$row->date);
      echo 
nl2br("\n");
      echo 
nl2br("\n");
      print 
$row->blog_mes;

}

// Some paginananananananation

echo '<div>';
for (
$i 1$i <= $numPages$i++)
{
    if (
$pageNum != $i)
    echo 
'<a href="index.php?id='$i'">'$i'</a> ';
    else
    echo 
'<b>'$i'</b>';
}
echo 
'</div>';

?>
cheers
__________________
My Recipe forum...don't click here
Last Blog Entry: Basic Advice for newbies (Feb 1st, 2008)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:56.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43