samedi 31 octobre 2015

Smarter way to write my newbie repetitive PHP?

Hi there please forgive me I've just managed to cobble this together and I think a more advanced programmer could give me a hint or two on a programming technique to improve it. I'm naming a ton of variables and repeating code like a madman.

The script is a html dashboard that controls database values to insert into ~160 different website titles/descriptions.

The dashboard code is:

$displayquery1 = "SELECT * from seo where seo_id = 1";
$displayresult1 = mysql_query($displayquery1, $con);

//fetch and print record for page 1

while ($row = mysql_fetch_assoc($displayresult1, MYSQL_NUM)) {

echo '<td align="left"><a href="' . $row[2] . '">' . $row[2] . '</a></td>'
. '<td><input type="text" name="seo1a" value=" ' .  $row[3] . '"></td>'
. '<td><input type="text" name="seo1b" value=" ' .  $row[4] . '"></td>   '
. '<td><input type="textarea" name="seo1c" value=" ' .  $row[5] . '"></td>    
</tr><br>';}            


//fetch and print record for page 2

$displayquery2 = "SELECT * from seo where seo_id = 2";
$displayresult2 = mysql_query($displayquery2, $con);

while ($row2 = mysql_fetch_assoc($displayresult2, MYSQL_NUM)) {

echo '<td align="left"><a href="' . $row2[2] . '">' . $row[2] . '</a></td>'
. '<td><input type="text" name="seo1a" value=" ' .  $row2[3] . '"></td>'
. '<td><input type="text" name="seo1b" value=" ' .  $row2[4] . '"></td>   '
. '<td><input type="textarea" name="seo1c" value=" ' .  $row2[5] . '"></td>    
    </tr><br>';}

and the insert script is:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   if (empty($_POST['seo1a'])) {
        echo 'please enter a seo title';
    } else {
        $seo1a = ($_POST['seo1a']);
        $seo1b = ($_POST['seo1b']);
        $seo1c = ($_POST['seo1c']);
        $seo2a = ($_POST['seo2a']);
        $seo2b = ($_POST['seo2b']);
        $seo2c = ($_POST['seo2c']);

        //create queries
    $q5 = "UPDATE seo SET seo_title='$seo1a', seo_description ='$seo1b', seo_content='$seo1c'  WHERE seo_id = 1";
    $q6 = "UPDATE seo SET seo_title='$seo2a', seo_description ='$seo2b', seo_content='$seo2c'  WHERE seo_id = 2";
    $r = mysql_query($q5, $con);
    $r2 = mysqli_query($q6, $con);
    }};

see how I'm naming every single variable and running the queries so repetitively? I have to do this for 160 pages. The way I'm getting the variables from the db is also probably wrong:

//get seo title for first page
$varquery1 = "SELECT * from seo where seo_id = 1";
$varresult1 = mysql_query($varquery1, $con);
if ($varresult1) {
$row1 = mysql_fetch_assoc(mysql_query($varresult1));
$seotitle = row1['seo_title'];
}

//get seo title for second page
$varquery2 = "SELECT * from seo where seo_id = 2";
$varresult2 = mysql_query($varquery2, $con);
if ($varresult2) {
$row2 = mysql_fetch_assoc(mysql_query($varresult2));
$seotitle = row2['seo_title'];
}

Please feel free to excoriate me for the quality of my code just give me a hint or two as well please. Also its not my website so I can't switch to mysqli just yet. Thanks very much

Aucun commentaire:

Enregistrer un commentaire