Jump to content

How to Avoid Duplicate Code


chaseman

Recommended Posts

I have a sorting functionality, the code is here:

 

<?php
$select_category = $_REQUEST['sort_category'];
$sort_date_var = $_REQUEST['sort_date'];
$sort_submit = $_POST['sortSubmit'];

// Connect to the database 
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if (($select_category == 'All') || (!isset($select_category)) && (!isset($sort_date_var))) {

// Retrieve the chosen category from MySQL
$query	= "SELECT * FROM con";
$data = mysqli_query($dbc, $query);

//Loop through the array of data
while ($row = mysqli_fetch_array($data)) 
{
	echo "<table class='knuffixTable'>";

	// Display the score data
	echo "<tr><td class='knuffix_name'>";
	echo "<strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>";
	echo "<tr><td class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>";
	echo "<tr><td class='knuffix_categoryDate'>" . $row['category'] . " | " . date('M d, Y', strtotime($row['contributed_date'])) . " </td></tr>";
	echo "</table>";
}
mysqli_close($dbc);


} elseif (isset($select_category) && !isset($sort_date_var)) {

// Retrieve the chosen category from MySQL
$query = "SELECT * FROM con WHERE category = '$select_category'";
$data = mysqli_query($dbc, $query) or die (mysqli_error($dbc));

//Loop through the array of data
while ($row = mysqli_fetch_array($data)) 
{
	echo "<table class='knuffixTable'>";

	// Display the score data
	echo "<tr><td class='knuffix_name'>";
	echo "<strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>";
	echo "<tr><td class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>";
	echo "<tr><td class='knuffix_categoryDate'>" . htmlentities($row['category']) . " | " . date('M d, Y', strtotime($row['contributed_date'])) . " </td></tr>";
	echo "</table>";
}
mysqli_close($dbc);

} elseif (!isset($select_category) && isset($sort_date_var)) {

// Retrieve the chosen category from MySQL
$query = "SELECT * FROM con ORDER BY contributed_date $sort_date_var";
$data = mysqli_query($dbc, $query) or die (mysqli_error($dbc));

//Loop through the array of data
while ($row = mysqli_fetch_array($data)) 
{
	echo "<table class='knuffixTable'>";

	// Display the score data
	echo "<tr><td class='knuffix_name'>";
	echo "<strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>";
	echo "<tr><td class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>";
	echo "<tr><td class='knuffix_categoryDate'>" . htmlentities($row['category']) . " | " . date('M d, Y', strtotime($row['contributed_date'])) . " </td></tr>";
	echo "</table>";
}
mysqli_close($dbc);

} elseif (isset($select_category) && isset($sort_date_var)) {

// Retrieve the chosen category from MySQL
$query = "SELECT * FROM con WHERE category = '$select_category' ORDER BY contributed_date $sort_date_var";
$data = mysqli_query($dbc, $query) or die (mysqli_error($dbc));

//Loop through the array of data
while ($row = mysqli_fetch_array($data)) 
{
	echo "<table class='knuffixTable'>";

	// Display the score data
	echo "<tr><td class='knuffix_name'>";
	echo "<strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>";
	echo "<tr><td class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>";
	echo "<tr><td class='knuffix_categoryDate'>" . htmlentities($row['category']) . " | " . date('M d, Y', strtotime($row['contributed_date'])) . " </td></tr>";
	echo "</table>";
}
mysqli_close($dbc);

}

?>

 

 

With this code I'm able to:

- showcase every category by DEFAULT when someone comes to the page.

- sort only by category,

- sort only by date,

- sort by category AND date,

 

The problem is as you can see I have a lot of duplicate code, if I for example want to change the table that is being printed out, I have to change it on all of them. I'd like to ask how I could avoid this duplicate code. Can I for example just have the table ONE TIME at a separate place and then insert the REFERENCE _after_ the query in each if statement?

 

Like this:

 

if (($select_category == 'All') || (!isset($select_category)) && (!isset($sort_date_var))) {

// Retrieve the chosen category from MySQL
$query	= "SELECT * FROM con";
$data = mysqli_query($dbc, $query);

table_here();


} elseif (isset($select_category) && !isset($sort_date_var)) {

// Retrieve the chosen category from MySQL
$query = "SELECT * FROM con WHERE category = '$select_category'";
$data = mysqli_query($dbc, $query) or die (mysqli_error($dbc));

table_here();

}

 

 

I'm just coding since 2 month so I don't know how do it, maybe I can do it with functions?

 

I've tried doing it with functions, but I got an error since the function doesn't contain any query statement and only the table because it was trying to fetch.

 

it looked like this:

 


function table_here () {

//Loop through the array of data
while ($row = mysqli_fetch_array($data)) 
{
	echo "<table class='knuffixTable'>";

	// Display the score data
	echo "<tr><td class='knuffix_name'>";
	echo "<strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>";
	echo "<tr><td class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>";
	echo "<tr><td class='knuffix_categoryDate'>" . $row['category'] . " | " . date('M d, Y', strtotime($row['contributed_date'])) . " </td></tr>";
	echo "</table>";
}
mysqli_close($dbc);

}

Link to comment
Share on other sites

Ok I solved this problem, this is how I got it to work. This  is a small experiment code that I used to figure it out:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="submit" />
</form>
<?php
include ('connectvars.php');
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$submit = $_POST['submit'];

function submit_test ($query) {

    $dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$data = mysqli_query($dbc, $query) or die (mysqli_error($dbc));

//Loop through the array of data
while ($row = mysqli_fetch_array($data)) 
{
	echo "<table class='knuffixTable'>";

	// Display the score data
	echo "<tr><td class='knuffix_name'>";
	echo "<strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>";
	echo "<tr><td class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>";
	echo "<tr><td class='knuffix_categoryDate'>" . $row['category'] . " | " . date('M d, Y', strtotime($row['contributed_date'])) . " </td></tr>";
	echo "</table>";
}

}


if ($submit) {

// Retrieve the chosen category from MySQL
$query	= "SELECT * FROM con";


submit_test ($query);

}

?>

 

 

Doing experiments is fun lol.

 

 

Important things to consider:

 

- induce the query into the function, otherwise you'll get an empty query error, since the function can't grab the variable outside the function

- induce the database connection information (in my case $dbc) into the function, since the function can (again) not grab outside the function, that why it would not know the password and username for the database connection

 

Once you got that, it should work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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