putting a long query and results into a function, help?
Posted: Sat Mar 13, 2010 6:40 pm
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
[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