If this is your first visit, check our documentation. To post, register or login.
Forums / General Questions / Question For PHP GURUS

Question For PHP GURUS

Started by philip937 on 6 Apr 2013, 12:55 PM · Views: 667

Results 1 to 10 of 10
6 Apr 2013, 12:55 PM
#1
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Question For PHP GURUS

I am using the better together mod and am calling some cross sells from my existing Multi cross sells mod database table.

Originally I had this code which worked

$resultxsells = mysql_query("SELECT products_id, xsell_id FROM products_xsell1");
while($rowxsells = mysql_fetch_array($resultxsells)) {
$this->add_prod_to_prod($rowxsells['products_id'], $rowxsells['xsell_id'], "X", 0);
}

but after I read a post that DrByte said about while loops and having to be careful they don't spiral out of control i've now changed it to this:

$resultxsells = mysql_query("SELECT products_id, xsell_id FROM products_xsell1");
$rowxsells = mysql_fetch_array($resultxsells);
$countthem = count($rowxsells);
$f = 0;
while ($f < $count) {
$this->add_prod_to_prod($rowxsells['products_id'], $rowxsells['xsell_id'], "X", 0);
$f++;
}

can anyone advise if this is now a safe way of doing it? Many thanks

Phil Rogers
A problem shared is a problem solved.

6 Apr 2013, 1:29 PM
#2
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Question For PHP GURUS

Who is $count in:

while ($f < $count) { 

Is that suppose to be $countthem perhaps?

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

6 Apr 2013, 1:36 PM
#3
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Question For PHP GURUS

Ajeh:

Who is $count in:

while ($f < $count) {

> 
> Is that suppose to be $countthem perhaps?

phew well spotted yes your right! so with that corrected is that a safe way to implement Ajeh?

Phil Rogers
A problem shared is a problem solved.

6 Apr 2013, 1:54 PM
#4
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Question For PHP GURUS

It will make sure that the while terminates when $f equals $countthem ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

6 Apr 2013, 2:07 PM
#5
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Question For PHP GURUS

oh no, actually this doesn't work :

$resultxsells = mysql_query("SELECT products_id, xsell_id FROM products_xsell1");
$rowxsells = mysql_fetch_array($resultxsells);
$countthem = count($rowxsells);
$f = 0;
while ($f < $countthem) {
$this->add_prod_to_prod($rowxsells['products_id'], $rowxsells['xsell_id'], "X", 0);
$f++;
}

can anyone advise the best way to do it? I have reverted back for now to the first method

Phil Rogers
A problem shared is a problem solved.

6 Apr 2013, 6:27 PM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: Question For PHP GURUS

philip937:

but after I read a post that DrByte said about while loops and having to be careful they don't spiral out of control i've now changed it to this:
While my point is universally valid, I'm certain I made that statement in the context of using Zen Cart's database access methods. However, you're bypassing that and making mysql calls directly.

In your code you've added a $f variable presumably to be a counter, but you're doing nothing with it in your "while" loop.

Recommendation: use Zen Cart's database functions and follow the pattern of including a MoveNext() call inside your "while" loop when iterating through the results obtained from making the call to the database using Zen Cart's database functions.
If you're going to skip the Zen Cart stuff and just write your own mysql functions, then use the PHP manual as your guide, and be prepared to know what you're doing when you encounter a clash between your own stuff and the Zen Cart stuff.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

6 Apr 2013, 6:36 PM
#7
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Question For PHP GURUS

DrByte:

While my point is universally valid, I'm certain I made that statement in the context of using Zen Cart's database access methods. However, you're bypassing that and making mysql calls directly.

In your code you've added a $f variable presumably to be a counter, but you're doing nothing with it in your "while" loop.

Recommendation: use Zen Cart's database functions and follow the pattern of including a moveNext() call inside your "while" loop when iterating through the results obtained from making the call to the database using Zen Cart's database functions.
If you're going to skip the Zen Cart stuff and just write your own mysql functions, then use the PHP manual as your guide, and be prepared to know what you're doing when you encounter a clash between your own stuff and the Zen Cart stuff.

I tried the move next stuff first as I've use that elsewhere but for some reason it throws the error stating it doesn't know about the function etc? Do I need to include another file to make use of te function? That why I'm calling directly. but you've pointed out something I missed, I'm missing the if statement should be
(if $f < $countthem)

I'm such a noob.

Any advice how to implement te zen code in te file would be appreciated.

It's in the ot_better_togetger file in includes/modules/order_total

Thanks

Phil Rogers
A problem shared is a problem solved.

6 Apr 2013, 6:45 PM
#8
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: Question For PHP GURUS

The general syntax is this:

global $db;  // this is needed if you're inside a function or class. Not needed in admin pages, unless inside functions or classes.
$result = $db->Execute("SELECT blah blah blah");
$number_of_records_found = $result->RecordCount(); // this number isn't really needed if you're using the !EOF to loop and don't really need to know the number of results for any other reason
while (!$result->EOF) {
  $i[] = $result->fields['the_db_field_name_whose_value_you_want_to_assign_to_$i'];
  $result->MoveNext();
}
// the extracted data will then be in the $i array.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

6 Apr 2013, 7:46 PM
#9
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Question For PHP GURUS

DrByte:

The general syntax is this:

global $db; // this is needed if you're inside a function or class. Not needed in admin pages, unless inside functions or classes.
$result = $db->Execute("SELECT blah blah blah");
$number_of_records_found = $result->RecordCount(); // this number isn't really needed if you're using the !EOF to loop and don't really need to know the number of results for any other reason
while (!$result->EOF) {
$i[] = $result->fields['the_db_field_name_whose_value_you_want_to_assign_to_$i'];
$result->MoveNext();
}
// the extracted data will then be in the $i array.

Thanks I think I must have been missing the global $db; as I'm pretty sore the error was referring to the $db->Execute

Will give it a go tomorrow thanks

Phil Rogers
A problem shared is a problem solved.

7 Apr 2013, 8:10 AM
#10
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Question For PHP GURUS

My working solution thanks to DrByte :)

global $db;
$resultxsells = $db->Execute("SELECT products_id, xsell_id FROM products_xsell1");
while (!$resultxsells->EOF) {
$this->add_prod_to_prod($resultxsells->fields['products_id'], $resultxsells->fields['xsell_id'], "X", 0);  
  $resultxsells->MoveNext();
}

Phil Rogers
A problem shared is a problem solved.