putting a long query and results into a function, help?

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

Post Reply
glennnall
Forum Newbie
Posts: 13
Joined: Wed Jan 27, 2010 7:54 am

putting a long query and results into a function, help?

Post by glennnall »

i have a LOT of if statements/switches that will all run the same query but with a couple of different values, namely this:
[syntax=php]$query = "SELECT * FROM ".$prefix."pagetops WHERE page_type='".$querypageid."'";
$mainresult = mysql_query($query);
if (!$mainresult) {
die(mysql_error());
}


while ($row = mysql_fetch_assoc($mainresult)) {
$page_heading = $row['page_heading'];
$page_desc = $row['page_desc'];
$prod_name_head = "Part #";
$prod_dim_head = "Dimensions";
$prod_desc_head = "Description";
$prod_image_head = "Picture";
$prod_link_head = "Price";



$pagetext = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" id=\"page_table\">";
$pagetext .= "<tr><td class=\"td_top\">";
$pagetext .= $page_heading;
$pagetext .= "</td></tr>";
$pagetext .= "<tr><td class=\"td_top\">";
$pagetext .= $page_desc;
$pagetext .= "</td></tr></table>";
$pagetext .= "<br />";


$pagetext .= "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" id=\"page_table\">";
$pagetext .= "<tr><td class=\"td_headings\" width=\"110\">";
$pagetext .= $prod_name_head;
$pagetext .= "</td><td class=\"td_headings\" width=\"110\">";
$pagetext .= $prod_dim_head;
$pagetext .= "</td><td class=\"td_headings\" width=\"315\">";
$pagetext .= $prod_desc_head;
$pagetext .= "</td><td class=\"td_headings\" width=\"145\">";
$pagetext .= $prod_image_head;
$pagetext .= "</td><td class=\"td_headings\" width=\"100\">";
$pagetext .= $prod_link_head;
$pagetext .= "</td></tr>";

}



$query = "SELECT * FROM ".$prefix."prods WHERE page_type='".$querypageid."'";
$mainresult = mysql_query($query);
if (!$mainresult) {
die(mysql_error());
}

while ($row = mysql_fetch_assoc($mainresult)) {
$part_no = $row['part_no'];
$prod_dim = $row['prod_dim'];
$prod_desc = $row['prod_desc'];
$prod_image = $row['img'];
$prod_link = $row['link'];
$prod_mfr = $row['mfr'];



$pagetext .= "<tr><td class=\"td_left\">";
$pagetext .= $prod_mfr." <br /> ".$part_no;
$pagetext .= "</td><td>";
$pagetext .= "dimensions here...";
$pagetext .= "</td><td>";
$pagetext .= $prod_desc;
$pagetext .= "</td><td align=\"center\">";
$pagetext .= "<img src=\"".$prod_image."\" />";
$pagetext .= "</td><td>";
$pagetext .= "<a href=\"".$prod_link."\">Click Here</a>";
$pagetext .= "</td></tr>";
}

$pagetext .= "</table>";[/syntax]

the only difference being the variables, of course.


i don't want my file full of that long code repeated so many number of times, so i'd like to put it all in a function, or something that i can call under these conditions... does that make sense?

can someone show me how i'd do that? i'd try to create a function with it, but i'm not sure how i'd get the values passed to the variables in the query as a function.

i'd surely appreciate any help someone might offer...

regards,
gn
Last edited by glennnall on Sun Mar 14, 2010 4:59 am, edited 2 times in total.
glennnall
Forum Newbie
Posts: 13
Joined: Wed Jan 27, 2010 7:54 am

Re: putting a long query and results into a function, help?

Post by glennnall »

all right, i'm going to attempt some clarification - i'm somewhat convoluted, it seems:

i have a long series of if() conditions, MOST of which will call this long query:

Code: Select all

 
 
 
if ($var == "A") {
 
// the LONG mysql_query using the changing variable $querypageid >>
$query = "SELECT * FROM ".$prefix."pagetops WHERE page_type='".$querypageid."'";
$mainresult = mysql_query($query);
if (!$mainresult) {
    die(mysql_error());
}
 
 
while ($row = mysql_fetch_assoc($mainresult)) {
LONG OUTPUT
 
} elseif ($var == "B") {
 
// the LONG mysql_query using the changing variable $querypageid >>
$query = "SELECT * FROM ".$prefix."pagetops WHERE page_type='".$querypageid."'";
$mainresult = mysql_query($query);
if (!$mainresult) {
    die(mysql_error());
}
 
 
while ($row = mysql_fetch_assoc($mainresult)) {
LONG OUTPUT
 
} elseif ($var == "C") {
 
// the LONG mysql_query using the changing variable $querypageid >>
$query = "SELECT * FROM ".$prefix."pagetops WHERE page_type='".$querypageid."'";
$mainresult = mysql_query($query);
if (!$mainresult) {
    die(mysql_error());
}
 
 
while ($row = mysql_fetch_assoc($mainresult)) {
LONG OUTPUT
 
} elseif ($var == "D") { ...
 
 
 
i'm trying to prevent that LONG mysql_query from being repeated over and over MAKING THE FILE VERY LONG when i'm sure it can be written as a function or something and just called like so:

Code: Select all

 
 
 
if ($var == "A") {
 
mysql_function($querypageid);
 
}
 
 
i think that makes more sense. i'm just trying to get that long query in my first post stuck into some kind of function i can call; i appreciate anyones help...
Post Reply