The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. December 05, 2008, 10:30:05 AM

Login with username, password and session length


Pages: 1 [2] 3
  Print  
Author Topic: OK guys some help needed - PHP and mysql  (Read 1212 times)
nutballs
Administrator
Lifer
*****
Online Online

Posts: 3455


View Profile
« Reply #15 on: July 09, 2008, 01:15:32 PM »

Perk followed your code above and it is throwing the error everytime - looks like it is something to do with this line:

echo ("<DD><A HREF='{$row["affurl"]}'>$row["disccode"][/url]</DD>\n");

thats cause perk dipshitted the code. Probably wrote it on his iPhone waiting in line for the 2.0...  Roll Eyes

should be:
Code:
echo ("<DD><A HREF='{$row["affurl"]}'>{$row["disccode"]}</A></DD>\n");

and frankly i hate single quote hrefs so i would actually do it as
Code:
echo ("<DD><A HREF=\"{$row["affurl"]}\">{$row["disccode"]}</A></DD>\n");
Logged
kopkingmeister
Rookie
**
Offline Offline

Posts: 16


View Profile
« Reply #16 on: July 09, 2008, 01:35:15 PM »

cheers nutballs - works a treat!

thanks guys

kk
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5230


:sniffle: Humor was so much easier before.


View Profile
« Reply #17 on: July 09, 2008, 03:27:08 PM »

Whoopsie! Sorry Kops! Nuts was close, but I'm not in line yet. Gonna wait for a month or so. Thanks Nuts for the patch  Praise

Cheers boys
Logged

If I can't be Mr. Root then I don't want to play.
nutballs
Administrator
Lifer
*****
Online Online

Posts: 3455


View Profile
« Reply #18 on: July 09, 2008, 03:37:28 PM »

and the student becomes the master...

<que starwars march or kungfu theme>
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5230


:sniffle: Humor was so much easier before.


View Profile
« Reply #19 on: July 09, 2008, 03:39:01 PM »

Awright awright  Sarcasm

ROFLMAO
Logged

If I can't be Mr. Root then I don't want to play.
kopkingmeister
Rookie
**
Offline Offline

Posts: 16


View Profile
« Reply #20 on: July 14, 2008, 01:08:16 PM »

Quick question guys

if I have multiple instances of the code:
<?
   
$id = $_GET['theparam'];

if ($retid = mysql_db_query($db, "SELECT * FROM discount_codes where id=$id", $cid))
{
   while ($row = mysql_fetch_array($retid))
   echo ("<DD><A HREF='{$row["affurl"]}'>$row["disccode"][/url]</DD>\n");
} else echo mysql_error();

?>

Do I need to include:

$id = $_GET['theparam'];

each time? - or can I include this just once at the top of the page?

Logged
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Online Online

Posts: 636



View Profile
« Reply #21 on: July 14, 2008, 01:09:57 PM »

Only once, when the page is loaded.

If you're using the code multiple times, you ought to just combine it into a function.
Logged

kopkingmeister
Rookie
**
Offline Offline

Posts: 16


View Profile
« Reply #22 on: July 14, 2008, 01:32:19 PM »

So would that be something like:

function Huh?Huh?Huh???($var1 = "execute the mysql call here")

Then when I want to echo the sql call:

{
  echo($var1);
}

or am I way off?
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5230


:sniffle: Humor was so much easier before.


View Profile
« Reply #23 on: July 14, 2008, 01:34:13 PM »

Kops it looks as though you'd be doing the same query and producing the same HTML every time... perhaps a better avenue would be to do it once and store the HTML into a global variable for later use. Consider:

Code:
<?

createList();

echo <<<HTML
<html><body>

Here's the list: {$GLOBALS['theList']}<br><br><br>

And here is is again! {$GLOBALS['theList']}
</body></html>
HTML;

function createList()
{
global $db; // assume the $db var is already created...

$id = $_GET['theparam'];

if (!preg_match('/^[0-9]+$/', $id)) die('Eat tofu and squirm, you vile lump of scriptkiddie.');

$retID = mysql_db_query("select * from discount_codes where id=$id");
while ($row = mysql_fetch_assoc($retID))
$outArr[] = "<DD><A HREF='{$row["affurl"]}'>{$row["disccode"]}[/url]</DD>";
$GLOBALS['theList'] = implode(chr(10), $outArr);
}
?>

(please note, I did this fast and did not check for accuracy)
« Last Edit: July 14, 2008, 01:35:49 PM by perkiset » Logged

If I can't be Mr. Root then I don't want to play.
kopkingmeister
Rookie
**
Offline Offline

Posts: 16


View Profile
« Reply #24 on: July 14, 2008, 02:01:15 PM »

cheers guys

Perks - please bear with me - I am sure this is all really basic stuff, but:

global $db; // assume the $db var is already created...

i assume you need to create a function which will call the global db?
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5230


:sniffle: Humor was so much easier before.


View Profile
« Reply #25 on: July 14, 2008, 02:51:06 PM »

In your example Kops, you referenced a variable $db, which I assumed was a handle to an open database. By saying "global $db" inside of the function call, I can access that variable. If I did not say that, then the $db variable would be null at the beginning of the function - in other words, it would not be a handle to a valid, open Database connection.

So somewhere else, you probably said something like "$db = mysql_connect(blah, blah, blah);" and we're using the $db variable here. The $db variable is, at that point, available to everything else that is "at the same level" or "in the same place" or "in the same scope" as that variable. The code inside of a function is not "in the same place" or "in the same scope" as where the $db variable was created, so we don't have access to it, UNLESS we declare that it is global - and that is done by saying "global $db;"

Hope that makes sense, scope issues are pretty weird for n00bs. No worries for lots of questions, I think we all understand here pretty well Wink
Logged

If I can't be Mr. Root then I don't want to play.
kopkingmeister
Rookie
**
Offline Offline

Posts: 16


View Profile
« Reply #26 on: August 14, 2008, 02:08:57 PM »

sorry guys not been around for a while - work has got in the way!

Now starting to dig back into this project again.

I have built all the up front input forms and database display so I can add records to the database and query the database to produce a list of all the records - which was just what I wanted.

Now am back on the actual page that will display the discount codes dynamically.

Been trying to get my head around the code above Perk and am really just not getting it!

Here is where my head is at right now.

The page that displays the dynamic data needs to have the following:

code to open the db connection - Ok get that bit
code to query the url string to identify the id number - i.e. www.discountcodesite.com/blahblah/display_data.php?id=XX
Then a number of calls at different parts of the page to get the following pieces of information relevant to that id number:

Page Title
ALT Keywords
ALT Description
Retailer name
discount code
date
etc
etc etc

In my initial example I was querying the url string each time I wanted to display a different parameter - i.e. ALT keywords, discount code etc etc

But from what you are saying I only need to query the url string for the id reference once and then I should be able to call each inidividual parameter wherever I need it on the page?

Sorry!
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5230


:sniffle: Humor was so much easier before.


View Profile
« Reply #27 on: August 14, 2008, 02:26:32 PM »

sorry guys not been around for a while - work has got in the way!
Right Kops. You only come 'round when it's time for a booty codie call  ROFLMAO
(j/k mate ... nice to have you around)

code to open the db connection - Ok get that bit
code to query the url string to identify the id number - i.e. www.discountcodesite.com/blahblah/display_data.php?id=XX
Then a number of calls at different parts of the page to get the following pieces of information relevant to that id number:

Page Title
ALT Keywords
ALT Description
Retailer name
discount code
date

Assuming that all the above information is in a single table (so that we don't have to discuss JOINs in this post) it's fairly straightforward and you've already pretty much gotten there.

First, open the database. Looks like you're there. Now the ID of the item you want will be in the $_GET array - that is a global array provided by PHP which gives you access to everything in the querystring. It is an associative array - so using
Code:
$theID = $_GET['id']
would set $theID to 'XX' (based on your example above). Now you'll want to throw a SQL query to grab the data record that has your data. your function will look something like this:
Code:
$handle = mysql_query("select * from itemtable where id='{$_GET['id']}'");
$row = mysql_fetch_assoc($handle);
...assuming, again, that you've done some cleaning of the URL as described above to make sure that JasonD or NOP doesn't have his way with you.

(Please note that I don't use the mysql_ functions anymore, I use my own class for database connections, so I am not 100% certain of my syntax - but the essence is correct)

Now, you could do a simple page output like this:
Code:
echo <<<HTML
Thank for visiting The Really Spammy Site.<br>
The item you're looking for is {$row['id']} described as {$row['caption']} and the price is {$row['price']}
HTML;

In this example I'm using a HEREDOC form ( <<< ) to output a big 'ol chunk of HTML, and references to the "row" array, that was populated when I did mysql_fetch_assoc.

Hope that makes more sense... good luck mate!

/p
Logged

If I can't be Mr. Root then I don't want to play.
kopkingmeister
Rookie
**
Offline Offline

Posts: 16


View Profile
« Reply #28 on: August 14, 2008, 02:35:01 PM »

i know I am sorry perk!!!!

Have been working away on the simpler bits of php to try and make it easier for me to understand these more detailed bits!

Plus as I said- little mater of the full time job getting in the way!!!
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5230


:sniffle: Humor was so much easier before.


View Profile
« Reply #29 on: August 14, 2008, 04:00:39 PM »

Ugh ... the whole "money" and "job" thing. Detestable.

NW mate, as I said, always welcome here and love to help.

/p
Logged

If I can't be Mr. Root then I don't want to play.
Pages: 1 [2] 3
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!