Parse an array

Fetching database records

Page 1 of 1

6 Replies - 8523 Views - Last Post: 11 January 2011 - 10:31 AM Rate Topic: -----

#1 _Leon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-December 10

Parse an array

Posted 10 January 2011 - 09:14 AM

hi
I'm working on an app that prints out a voucher for a user once they have made a purchase from the site.

The voucher looks like this. The voucher is populated dynamically with values from the database. There are some values missing though. Here's the template associated with the script in question.

...
<!--coupon_print.html-->

</div>
<!--endtop -->
<div id="teamtitle">{$team['product']}</div>
<!--main -->
<div id="main">

<div id="mleft">
<div id="name">VIP</div>
<div id="relname">{$login_user['username']}&nbsp;({$login_user['email']})</div>
<div id="name">{$INI['system']['couponname']} valid until:</div>
<div id="relname">${date('d-m-Y', $team['expire_time'])}</div>
<div id="name">Address:</div>
<div id="relname">{$partner['address']}</div>

<div id="coupon">
	<p>Voucher Number:&nbsp;<span id="voucher-id">{$coupon['id']}</span></p>
	<p>Voucher Code:&nbsp;<span id="voucher-secret">{$coupon['secret']}</span></p>
</div>

</div>
<!--right -->
<div id="mright">
<div id="goods-image"><img src=${team_image($team['image'])} width="270px" height="190px" /></div>
<div id="name">Tip</div>
<div id="notice">{$team['notice']}</div>
<div id="name">How to use this {$INI['system']['couponname']}</div>
<div id="notice">
<ul>
	<li>1. This voucher can only be used in:&nbsp;{$partner['title']}</li>
	<li>2. Print this voucher (There is a unique code for purchase)</li>
	<li>3. Make your purchase with this voucher; at the business; before it's due time.</li>
</ul>
</div>

</div>

<div style="clear:both;"></div>
</div>
<!--endmain -->

<div id="server">Business telephone:{$partner['phone']}&nbsp;&nbsp;Location:{$partner['address']}</div>




The script that generates/populates the template above is:

<?php #print.php
require_once(dirname(dirname(__FILE__)) . '/app.php');

need_login();

$id = strval($_GET['id']);
print '$id = '.$id.'<br />'; //prints 68990085 - debugging

$coupon = Table::Fetch('coupon', $id);

print '$coupon = ';
var_dump( $coupon).'<br />'; //debugging 

if (!$coupon) {
	Session::Set('error', "{$INI['system']['couponname']} doesnt exist.");
	redirect(WEB_ROOT . '/coupon/index.php');
}

if ($coupon['user_id'] != $login_user_id) { 
	Session::Set('error', "This order{$INI['system']['couponname']} is not yours");
	redirect(WEB_ROOT . '/coupon/index.php');
}


print '<br />';
print '$coupon[\'partner_id\'] = '.$coupon['partner_id'].'<br />'; //debugging

//if(isset($coupon['partner_id'])) {

//need_partner();//debug
$partner = Table::Fetch('partner', $coupon['partner_id']);

//}
print '$partner = ';
var_dump( $partner); //debugging

print '<br />';

$team = Table::Fetch('team', $coupon['team_id']);
print '$team[] = ';
var_dump($team);

$pagetitle = 'Print Coupon';
include template('coupon_print');





Now, the script above 'print.php' successfully retrieves values from the
$coupon
array - populating the voucher successfully. The problem lies with the
$partner
array. The script wont pull values from this array when the app executes the following line:

$partner = Table::Fetch('partner', $coupon['partner_id']);
, i.e.
var_dump( $partner);
returns: empty array i.e.
$partner = array(0){ }


Now my script passes in
$coupon['partner_id'] = 0
to the Fetch method, or
 Table::Fetch('partner', 0 );
where 'partner' is a table from the database. The Fetch method looks like:

// class Table {

static public function Fetch($n=null,$ids=array(),$k='id')
	{
		if ( empty($ids) || !$ids ) return array();
		$single = is_array($ids) ? false : true;

		settype($ids, 'array'); $ids = array_values($ids);
		$ids = array_diff($ids, array(NULL));

		if ($k=='id') { 
			$r = self::_Fetch($n, $ids);
			return $single ? array_pop($r) : $r;
		}

		$result = DB::LimitQuery($n, array(
					'condition' => array( $k => $ids, ),
					'one' => $single,
					));

		if ( $single ) { return $result; }
		return $result;
	}



I don't fully understand the Fetch() algorithm, but i follow that a database table 'partner' is passed in as an argument. The structure of partner is:

CREATE TABLE IF NOT EXISTS `partner` (
  `id` bigint(20) unsigned NOT NULL auto_increment,
  `username` varchar(32) default NULL,
  `password` varchar(32) default NULL,
  `title` varchar(128) default NULL,
  `group_id` int(10) unsigned NOT NULL default '0',
  `homepage` varchar(128) default NULL,
  `city_id` int(10) unsigned NOT NULL default '0',
  `bank_name` varchar(128) default NULL,
  `bank_no` varchar(128) default NULL,
  `bank_user` varchar(128) default NULL,
  `location` text NOT NULL,
  `contact` varchar(32) default NULL,
  `image` varchar(128) default NULL,
  `image1` varchar(128) default NULL,
  `image2` varchar(128) default NULL,
  `phone` varchar(18) default NULL,
  `address` varchar(128) default NULL,
  `other` text,
  `mobile` varchar(12) default NULL,
  `open` enum('Y','N') NOT NULL default 'N',
  `enable` enum('Y','N') NOT NULL default 'Y',
  `head` int(10) unsigned NOT NULL default '0',
  `user_id` int(10) unsigned NOT NULL default '0',
  `create_time` int(10) unsigned NOT NULL default '0',
  `longlat` varchar(255) default NULL,
  `display` enum('Y','N') NOT NULL default 'Y',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `UNQ_ct` (`city_id`,`title`),
  UNIQUE KEY `UNQ_u` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;







If I go to another part of the site i.e. 'http://todayscitydeals.com/biz/settings.php' (need to be logged in to reach this) and do some debugging, i.e.
var_dump($partner);
I can access the array fine, seen here:

$partner[ ]

Is this something to do with scope? I didn't write the app, but I'm pretty confident the group that did wouldn't have made an oversight of this magnitude.



Can anyone throw some light on this? :)

Is This A Good Question/Topic? 0
  • +

Replies To: Parse an array

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,238
  • Joined: 08-June 10

Re: Parse an array

Posted 10 January 2011 - 10:18 AM

Table::Fetch('partner', 0 ); returns array() because you pass 0 as the second parameter. empty(0) returns true! (as does !0)

PS. delete line 21 in Table::Fetch, it doesn’t make sense.

This post has been edited by Dormilich: 10 January 2011 - 10:24 AM

Was This Post Helpful? 0
  • +
  • -

#3 _Leon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-December 10

Re: Parse an array

Posted 10 January 2011 - 11:48 AM

View PostDormilich, on 10 January 2011 - 09:18 AM, said:

empty(0) returns true! (as does !0)

PS. delete line 21 in Table::Fetch, it doesn’t make sense.



hi
Thanks for the reply. OK, I commented out line 21
//if ( $single ) { return $result; }
thanks.

Quote

Table::Fetch('partner', 0 ); returns array() because you pass 0 as the second parameter.

Sure, the call


static public function Fetch($n=null,$ids=array(),$k='id')
	{
		if ( empty($ids) || !$ids ) return array();
		$single = is_array($ids) ? false : true;




where
$n='partner', $ids=0, $k='id'
would result in
empty($ids) = true
hence returning array(). I see that now, ta.


I dont think it's anything to do with the actual method declaration. If i can see the array contents of
$partner[]
when i do a 'var_dump()' in
http://todayscitydeals.com/biz/settings.php
, then why can't i see the same array when i do a var_dump() in
http://todayscitydeals.com/coupon/print.php?id=PDFSEfKYYt
??

Don't understand..:)

This post has been edited by _Leon: 10 January 2011 - 11:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,238
  • Joined: 08-June 10

Re: Parse an array

Posted 10 January 2011 - 12:31 PM

you have 2 different IDs, thus you (possibly) have 2 different $partner arrays (id => $coupon => $partner)
Was This Post Helpful? 0
  • +
  • -

#5 _Leon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-December 10

Re: Parse an array

Posted 11 January 2011 - 03:55 AM

View PostDormilich, on 10 January 2011 - 11:31 AM, said:

you have 2 different IDs, thus you (possibly) have 2 different $partner arrays (id => $coupon => $partner)


Interesting thought, and i can see your logic. OK, my next step would be to try and disprove that statement. :) OK, over at: http://todayscitydea...iz/settings.php, which looks like:

<?php #settings.php
require_once(dirname(dirname(__FILE__)) . '/app.php');

need_partner();
$partner_id = abs(intval($_SESSION['partner_id']));
$login_partner = $partner = Table::Fetch('partner', $partner_id);

if ( $_POST ) {
	$table = new Table('partner', $_POST);
	$table->SetStrip('location', 'other');
	$table->SetPk('id', $partner_id);
	$update = array(
		'title', 'bank_name', 'bank_user', 'bank_no',
		'location', 'other', 'homepage', 'contact', 'mobile', 'phone',
		'address',
	);
	if ( $table->password == $table->password2 && $table->password ) {
		$update[] = 'password';
		$table->password = ZPartner::GenPassword($table->password);
	}
	$flag = $table->update($update);
	if ( $flag ) {
		Session::Set('notice', 'Partner info reset done!');
		redirect( WEB_ROOT . "/biz/settings.php");
	}
	Session::Set('error', 'Partner info reset failed');
	$partner = $_POST;
}

include template('biz_settings');


//debugging

$partner = Table::Fetch('partner', 2 );
var_dump($partner); //returns an array with all contents displayed.



Sure enough, there are two records in the partner table.

And:
$partner = Table::Fetch('partner', 2 );
$partner = Table::Fetch('partner', 1 )


works fine! i.e. displays:

array(26) { ["id"]=> string(1) "2" ["username"]=> string(4) "andy" ["password"]=> string(32) "15cb086c0061bbfd1605a8738adf2248" ["title"]=> string(19) "andys test business" ["group_id"]=> string(2) "12" ["homepage"]=> string(18) "http://nowhere.com" ["city_id"]=> string(2) "10" ["bank_name"]=> string(10) "Lloyds TSB" ["bank_no"]=> string(23) "46464646464646464646464" ["bank_user"]=> string(14) "Mr bank holder" ["location"]=> string(321) "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur" ["contact"]=> string(4) "andy" ["image"]=> string(33) "team/2010/1209/12919079711450.jpg" ["image1"]=> NULL ["image2"]=> NULL ["phone"]=> string(9) "000888666" ["address"]=> string(70) "675 - The Palace Arcade, Northern Quarter, City Centre, Manchester, M5" ["other"]=> string(313) "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur" ["mobile"]=> string(11) "57575757575" ["open"]=> string(1) "Y" ["enable"]=> string(1) "Y" ["head"]=> string(1) "0" ["user_id"]=> string(1) "1" ["create_time"]=> string(10) "1291907971" ["longlat"]=> string(37) "54.98773982819891,-1.5923309326171875" ["display"]=> string(1) "Y" } 



which is what i want! :)

Moreover, this code
$partner = Table::Fetch('partner', $coupon['partner_id'] );

(from the same url: /biz/settings.php ) returns:

array(0) { } 


not what i'm looking for.




And over here: http://todayscitydea...=WRMjc1OTYwMzQw, where print.php is listed above. The exact same function call:

$partner = Table::Fetch('partner', 2);
var_dump( $partner); 


returns the array i'm looking for i.e.: $partner which tells me, there is only one array
$partner
and that it is in fact a database table. After all the method:

Table::Fetch('partner', $coupon['partner_id']);


is a big clue to what the array is, no? 'Fetch a Table'. So, if
Table::Fetch('partner', 1 );
works globally, (by the looks of things), why won't

Table::Fetch('partner', $coupon['partner_id']);
work? That's the question. It's got to be something to do with the second parameter:

$coupon['partner_id']


no??

From print.php

<?php #print.php
require_once(dirname(dirname(__FILE__)) . '/app.php');
need_login();
$id = strval($_GET['id']);

print '$id = '.$id.'<br />'; //prints 68990085 - debugging

$coupon = Table::Fetch('coupon', $id);
...



a call to Fetch() has the following parameters;

$coupon = Table::Fetch('coupon', 68990085 );
...


where the coupon table looks like:this.


Well, there is only one record in the coupon table, with an id=68990085; Hence, $coupon is now an array with 15 fields. Still in print.php:

$partner = Table::Fetch('partner', $coupon['partner_id'] );
where $coupon['partner_id'] = 0. Now I just noticed that my previous successful calls to:


$partner = Table::Fetch('partner', $coupon['partner_id'] );
were when $coupon['partner_id'] =1, or $coupon['partner_id'] = 2. Ah, the $partner_id from the partner table doesn't match the $partner_id from the 'coupon' table. This is a flaw in the database schema no?

This post has been edited by _Leon: 11 January 2011 - 04:01 AM

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,238
  • Joined: 08-June 10

Re: Parse an array

Posted 11 January 2011 - 06:35 AM

View Post_Leon, on 11 January 2011 - 10:55 AM, said:

Well, there is only one record in the coupon table, with an id=68990085; Hence, $coupon is now an array with 15 fields. Still in print.php:

$partner = Table::Fetch('partner', $coupon['partner_id'] );
where $coupon['partner_id'] = 0. Now I just noticed that my previous successful calls to:

we already know that 0 causes the Fetch() method to return prematurely (unless you have changed the code)

View Post_Leon, on 11 January 2011 - 10:55 AM, said:

$partner = Table::Fetch('partner', $coupon['partner_id'] );
were when $coupon['partner_id'] =1, or $coupon['partner_id'] = 2. Ah, the $partner_id from the partner table doesn't match the $partner_id from the 'coupon' table. This is a flaw in the database schema no?

possibly, but I dont know how the tables shall relate to each other.
Was This Post Helpful? 1
  • +
  • -

#7 _Leon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-December 10

Re: Parse an array

Posted 11 January 2011 - 10:31 AM

View PostDormilich, on 11 January 2011 - 05:35 AM, said:

we already know that 0 causes the Fetch() method to return prematurely (unless you have changed the code)

Quote

possibly, but I don’t know how the tables shall relate to each other.


hi
Well i found an quick fix for the time being. The crux of it is that the
$coupon['partner_id']

doesn't match any id from the partners table, and thus no fields are returned. I changed the partner_id in the coupon table to match the partner_id in the partner table and that worked. The point is though, this should be done dynamcally through the app i.e. $coupon['partner_id'] = a specific id determined by the deal bought (if you follow me). I'm thinking that to get a real understanding of what's happening is to go through a live deal step by step making a note of the URI's and which methods/templates are being called. It's probably going to come down to something of the nature:

UPDATE table1
      SET table1.col = table2.col1
FROM table2 WHERE table2.oldCol = table1.col




this is of course a cut and paste job from some random site..:) Hey, thanks for the help dude..
best..

Oh no, i can't pull images from the $team array now....

This post has been edited by _Leon: 11 January 2011 - 10:33 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1