Need help with array

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

Rovch
Forum Newbie
Posts: 10
Joined: Wed Feb 01, 2012 8:26 pm

Need help with array

Post by Rovch »

Hey guys, I've been trying to make this array work properly and nothing worked so far.

As of right now it shows current and next month, and I want it to show next month and month after next.
I've tried skipping first item in array, but then it only shows one month instead of two (yes, I've raised array limit to 3).

Anyways, I'd really appreciate some help with this.

Here is the code:
<?php
$oneMonth = VarsItem::getVarsValue('oneMonthTravel');
$oneMonth == ($oneMonth == '') ? 1 : $oneMonth;
$monthAll = TravelItem::getAllMount(array('limit' => 2, 'start' => $oneMonth));
if (sizeof($monthAll) > 0)
{
$i = 0; ?>
<!-- распорка -->
<table border="0" cellspacing="0" cellpadding="0" width="520" align="center">
<tr valign="top">
<td width="100%"><img src="/image/0.gif" width="1" height="15" border="0" /></td>
</tr>
</table>
<a name="inquirer"></a>
<table border="0" cellspacing="0" cellpadding="0" width="520" align="center">
<tr valign="top">
<td align="center" colspan="2"><img src="/image/where.gif" width="505" height="16" border="0" /></td>
</tr>
<tr valign="top">
<?

foreach ($monthAll as $onemonth)
{

$i++;
?>
<td width="260">
<table border="0" cellspacing="0" cellpadding="0" width="100%" class="latestmain">
<tr><td colspan="2"><h3><%= strtoupper(monthAll($onemonth["month_id"],'namer')) %></h3></td></tr>
<?
$rowTravel = TravelItem::SelectAll(array('month_id' => $onemonth["month_id"], 'is_show' => 1));
foreach ($rowTravel as $onerowTravel)
{
$style ='style="padding-left:5px;"';
$partArray = checkEmptyPartitions($part, CountryItem::getCountryWorkName($onerowTravel['country_id']));
$hrefTravel = CountryItem::getCountryWorkName($onerowTravel['country_id'])."/".(($partArray["$part"] == 1) ? $part.'/' : ""); ?>
<tr valign="top">
<td><img src="/image/bullet-a.gif" width="9" height="9" border="0" style="margin-top:2px;" /></td>
<td width="100%" <%= $style %>><b><span id="orange"><%= CountryItem::getCountryName($onerowTravel['country_id']) %>:</span></b> <small><%= $onerowTravel['title'] %> <a href="<%= $hrefTravel %>">more...</a></small></td>
</tr>
<?
} ?>
</table>
</td>
<?
} ?>
</tr>
<tr valign="top">
<td colspan="<%= $i+1 %>">
<table border="0" cellspacing="0" cellpadding="0" width="100%" class="latestmain">
<tr><td><a href="travel.htm">where to go another month</a></td></tr>
</table>
</td>
</tr>
</table>
<?
} ?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with array

Post by Celauran »

Please post the code for

Code: Select all

TravelItem::getAllMount
Rovch
Forum Newbie
Posts: 10
Joined: Wed Feb 01, 2012 8:26 pm

Re: Need help with array

Post by Rovch »

Thanks for help, here is TravelItem::getAllMount

Code: Select all


...

  function getAllMount($parms = array()) 
  { 
    $limit = (isset($parms['limit'])) ? 'LIMIT '.$parms['limit'] : ''; 
    $where = (isset($parms['start'])) ? ' and month_id >= '.$parms['start'] : ''; 
    if ($parms['start'] == 12)  $where .= ' or month_id = 1'; // for december 
    $db =& Storage::DBI(); 
    return $db->query("SELECT DISTINCT month_id from travel WHERE is_show=1 ".$where." ORDER BY 1 ".($parms['start'] == 12 ? 'DESC' : '')." ".$limit); 
  } 

...

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with array

Post by Celauran »

That looks fine. What is the output of this?

Code: Select all

$monthAll = TravelItem::getAllMount(array('limit' => 3, 'start' => 1));
Rovch
Forum Newbie
Posts: 10
Joined: Wed Feb 01, 2012 8:26 pm

Re: Need help with array

Post by Rovch »

As of right now it's showing January and December.

It needs to show March and April.

I just realized, that now that it's a new month, the function did not update so it's showing month behind now.

Code: Select all

$monthAll = TravelItem::getAllMount(array('limit' => 3, 'start' => 1));
Output: January and December

Code: Select all

$monthAll = TravelItem::getAllMount(array('limit' => 4, 'start' => 2));
Output: December

Code: Select all

$monthAll = TravelItem::getAllMount(array('limit' => 5, 'start' => 3));
Output: December

I don't get why this doesn't work.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with array

Post by Celauran »

Those results are... surprising.

Does running the query manually yield the expected results?
Rovch
Forum Newbie
Posts: 10
Joined: Wed Feb 01, 2012 8:26 pm

Re: Need help with array

Post by Rovch »

Celauran wrote:Those results are... surprising.

Does running the query manually yield the expected results?
Exactly right, which is why I don't get why nothing I've tried so far worked. I've tried bunch of things and nothing gave me results I expected.

Here is full TravelItem class, maybe you can spot something I can't:

Code: Select all

<?php 
class TravelItem extends Storable { 

   function TravelItem($row = array()) {  
      Storage::Init(TravelItem::def(), $this->data); 
      $this->Storable($row);  
   } 


   function &def() { 
      return array( 
         'fields' => array( 
            'travel_id'  => 0, 
            'month_id'   => 0, 
            'country_id' => 0, 
            'title'      => '',  
            'ord'        => '', 
            'is_show'    => 1, 
             
         ), 
         'key'      => 'travel_id', 
         'auto_key' => true, 
         'auto_update' => array('is_show' => 0), 
         'table'    => 'travel'); 
   } 


   function &Load($id) {  
      if (!($row =& Storage::Load(TravelItem::def(), $id))) return false; 
      return new TravelItem($row); 
   } 

  
   function Store() { 
     $actionMail = ($this->data['travel_id'] > 0) ? 'edit' : 'add'; 
     $templateMail = 'travel'; 
     if (Storage::Store(TravelItem::def(), $this->data)) {   
       changeSite($actionMail,$templateMail,$this->data['travel_id']); 
       return true; 
     } else return false;    
   } 
    

   function Update(&$rows) {  
     $actionMail = 'update'; 
     $templateMail = 'travel'; 
     changeSite($actionMail,$templateMail,$rows); 
     return Storage::Update(TravelItem::def(), $rows);  
   } 

  
   function Delete(&$ids)  { 
     $actionMail = 'delete'; 
     $templateMail = 'travel'; 
     changeSite($actionMail,$templateMail,$ids); 
     return Storage::Delete(TravelItem::def(), $ids); 
   } 
    
   function getWhere(&$parms) { 
    $where = ""; 
    if (isset($parms['month_id'])) $where = "month_id='".$parms['month_id']."'"; 
    if (isset($parms['country_id'])) $where = (empty($where) ? "" : $where." and ")."country_id='".$parms['country_id']."'"; 
    if (isset($parms['is_show'])) $where = (empty($where) ? "" : $where." and ")."is_show='".$parms['is_show']."'"; 
    if (isset($parms['searchFilter'])) $where = (empty($where) ? "" : $where." and ")."title LIKE '%".$parms['searchFilter']."%'"; 
    $where = empty($where) ? "" : "WHERE ".$where; 
    return $where; 
  } 
   
  function getOrder(&$parms) { 
    $field = "month_id,ord*100,country_id"; 
    if (isset($parms['sort_field']) and !empty($parms['sort_field'])) { $field = $parms['sort_field']; } 
    return "ORDER BY $field"; 
  } 

  function SelectAll($parms = array()) { 
    $db =& Storage::DBI(); 
    return $db->query("SELECT * FROM travel ".TravelItem::getWhere($parms)." ".TravelItem::getOrder($parms), DBI::ExtractDBParms($parms, array('key' => 'travel_id'))); 
  } 
   
  function CountAll($parms = array()) { 
    $db =& Storage::DBI(); 
    return $db->queryValue("SELECT COUNT(*) FROM travel ".TravelItem::getWhere($parms)); 
  } 
   
  function getAllMount($parms = array()) 
  { 
    $limit = (isset($parms['limit'])) ? 'LIMIT '.$parms['limit'] : ''; 
    $where = (isset($parms['start'])) ? ' and month_id >= '.$parms['start'] : ''; 
    if ($parms['start'] == 12)  $where .= ' or month_id = 1'; // for december 
    $db =& Storage::DBI(); 
    return $db->query("SELECT DISTINCT month_id from travel WHERE is_show=1 ".$where." ORDER BY 1 ".($parms['start'] == 12 ? 'DESC' : '')." ".$limit); 
  } 
   
  function getTitle($travel_id) { 
     $db =& Storage::DBI(); 
     list($country_id,$month_id) = $db->queryList("SELECT country_id,month_id FROM travel WHERE travel_id =?",array("",""),Array($travel_id)); 
     return ($month_id > 0 ? monthAll($month_id,'namer') : "")." : ".CountryItem::getCountryName($country_id); 
  }  
   
  function getUpdateList($travel_id) {  
     $db =& Storage::DBI(); 
     return $db->queryList("SELECT is_show,ord,country_id,month_id FROM travel WHERE travel_id =?",array('','','',''),Array($travel_id)); 
  } 
} 
?>
Rovch
Forum Newbie
Posts: 10
Joined: Wed Feb 01, 2012 8:26 pm

Re: Need help with array

Post by Rovch »

Also, here is a screenshot of database table:

Image
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with array

Post by Celauran »

Is that the full table? Because that would explain things; only December and January have is_show = 1
Rovch
Forum Newbie
Posts: 10
Joined: Wed Feb 01, 2012 8:26 pm

Re: Need help with array

Post by Rovch »

Celauran wrote:Is that the full table? Because that would explain things; only December and January have is_show = 1
Yes sir, that's a full table. Although the way function output looks is:
In December:
*blah1
*blah2
*blah3

In January:
*blah 5
*blah6
*blah7
What that table holds is those blah1, blah2, blah3 and so on. I've tried changing 'is_show to 0' and all it does is gets rid of one of three outputs for December or January. I don't understand how this works, but month names aren't pulled from this table. This table just holds variables that are outputted for each given month if that make sense.

Here is how month name is obtained from a number (I think):

Code: Select all

<%= strtoupper(monthAll($onemonth["month_id"],'namer')) %>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with array

Post by Celauran »

Rovch wrote:I've tried changing 'is_show to 0'
In the query or in the table? What if you (temporarily, at least) remove the is_show clause from the query altogether? Is there any other logic, perhaps in the display code, that relies on is_show?
Rovch
Forum Newbie
Posts: 10
Joined: Wed Feb 01, 2012 8:26 pm

Re: Need help with array

Post by Rovch »

I've changed is_show in the table.

The only code that has to do with it is (as far as I can see):

Code: Select all

function &def() { 
      return array( 
         'fields' => array( 
            'travel_id'  => 0, 
            'month_id'   => 0, 
            'country_id' => 0, 
            'title'      => '',  
            'ord'        => '', 
            'is_show'    => 1, 
             

Code: Select all

return $db->query("SELECT DISTINCT month_id from travel WHERE is_show=1 ".$where." ORDER BY 1 ".($parms['start'] == 12 ? 'DESC' : '')." ".$limit);
If I removed 'is_show' it from the query, it would mess up the information that is output with each given month.

If I remove that colum in the table (or set it all to zero), my output will look like this because no row will have "is_show = 1":
In December:

In January:
I believe that the problem lies somewhere else, perhaps in another table or function that (I haven't found yet) deals with what two months are shown. The rest should just follow without a problem.

What really bothers me, is why this array does not yield the result that are manually input.

I am really lost now.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help with array

Post by Celauran »

Rovch wrote:What really bothers me, is why this array does not yield the result that are manually input.
Looks to me like it does.

Code: Select all

$monthAll = TravelItem::getAllMount(array('limit' => 3, 'start' => 1));
produces the query:

Code: Select all

SELECT DISTINCT month_id from travel WHERE is_show = 1 AND month_id >= 1 ORDER BY 1 LIMIT 3
Based on the table you posted, the result of this should be January and December.
Rovch
Forum Newbie
Posts: 10
Joined: Wed Feb 01, 2012 8:26 pm

Re: Need help with array

Post by Rovch »

Actually you're right, however what initially changes 'is_show' in the table?

Can I completely get rid of 'is_show' and use something like:

Code: Select all

$month = $date("n");	
return $db->query("SELECT DISTINCT month_id from travel WHERE month_id=".$month." ORDER BY 1 ".($parms['start'] == 12 ? 'DESC' : '')." ".$limit);
And hopefully not change anything else.
Rovch
Forum Newbie
Posts: 10
Joined: Wed Feb 01, 2012 8:26 pm

Re: Need help with array

Post by Rovch »

For some reason I can't get PHP to identify month number like this:

Code: Select all

date_format($date, "n")
Also, how would I go about getting two months instead of one out of this? So far I can only get one month to output properly.

Code: Select all

 return $db->query("SELECT DISTINCT month_id from travel WHERE month_id=".$month." ORDER BY 1 ".($parms['start'] == 12 ? 'DESC' : '')." ".$limit);
Post Reply