Jump to content

help creating a function? so i don't have to keep repeating this code


glennn.php

Recommended Posts

i have a LOT of if statements/switches that will all run the same query but with a couple of different values, namely this:

 

 


$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>";	

 

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

It not really a function you should be looking at (though you could still wrap your code within one), but an sql join. Your using two queries, where this can likely be done easily in one.

 

Take a look at this tutorial from the main site.

well, that's probably true, since i'm a novice in mysql as well, but what i'm describing (poorly) is a different issue. let me explain better:

 

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

 

 



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)) {...

} 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)) {...

} 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)) {...

} 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:

 

 



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...

 

 

 

Functions are simple. Define a name, and any arguments you wish to be passed into them. The argument then show up within the function as whatever you defined them to be.

 

function add($a, $b) {
  return $a + $b;
}

echo foo(20, 10);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.