Jump to content

Simplifying code so it not repeatable.


Russia

Recommended Posts

Hey I have this code thats basically repeating but the thing is Im adding a LIMIT 1+,1+ to each one since im echoing from the database row.

 

Here is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Example Form</title>
  <link rel="stylesheet" type="text/css" href="dd.css" />
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.dd.js"></script>
</head>
<body>


<!--
.style1 {
   font-family: Verdana, Arial, Helvetica, sans-serif;
   font-size: 12px;
   color: #000000;
   background-color: #FFFFCC;
   padding: 2px;
   height: 14px;
   width: 200px;
   border: 1px solid #7F9DB9;
}
--> 


<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
mysql_connect("localhost", "", "")or die("cannot connect");
mysql_select_db("test")or die("cannot select DB");
$tbl_name="test_mysql";
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

//FIRST ROW
$result0 = mysql_query("SELECT * FROM test_mysql")or die(mysql_error());  
$row0 = mysql_fetch_array($result0);

//SECOND ROW
$result1 = mysql_query("SELECT * FROM test_mysql LIMIT 1,1")or die(mysql_error());  
$row1 = mysql_fetch_array($result1);

//THIRD ROW
$result2 = mysql_query("SELECT * FROM test_mysql LIMIT 2,2")or die(mysql_error());  
$row2 = mysql_fetch_array($result2);


//FIRST ROW SHORTEN PARAGRAPH
$result0 = mysql_query("SELECT * FROM test_mysql")or die(mysql_error());  
$row0 = mysql_fetch_array($result0);
$paragraph0 =  $row0['message'];
$rough_short_par0 = substr($paragraph0, 0, 100);
$clean_short_par0 = substr($rough_short_par0, 0);
$clean_sentence_row1 = $clean_short_par0 . "...";

//SECOND ROW SHORTEN PARAGRAPH
$result1 = mysql_query("SELECT * FROM test_mysql LIMIT 1,1")or die(mysql_error());  
$row1 = mysql_fetch_array($result1);
$paragraph1 =  $row1['message'];
$rough_short_par1 = substr($paragraph1, 0, 100);
$clean_short_par1 = substr($rough_short_par1, 0);
$clean_sentence_row2 = $clean_short_par1 . "...";

//THIRD ROW SHORTEN PARAGRAPH
$result2 = mysql_query("SELECT * FROM test_mysql LIMIT 2,2")or die(mysql_error());  
$row2 = mysql_fetch_array($result2);
$paragraph2 =  $row2['message'];
$rough_short_par2 = substr($paragraph2, 0, 100);
$clean_short_par2 = substr($rough_short_par2, 0);
$clean_sentence_row3 = $clean_short_par2 . "...";

?>    
<br>
<?php
//$picture = array();
while($row = mysql_fetch_assoc($result))
{
$id = $row['id'];
switch ($row['icon']) {
    case 1:
        $picture[$id] = '<img src="http://www.nikibone.com/recipe/fruit/graphics/apples.jpg" title="apple" alt="apple" />';
        //echo $picture[$id];
        break;      
    case 2:
        $picture[$id] = '<img src="http://www.nikibone.com/recipe/fruit/graphics/bananas.jpg" title="banana" alt="banana" />';
        //echo $picture[$id];
        break;    
    case 3:
        $picture[$id] = '<img src="http://www.nikibone.com/recipe/fruit/graphics/oranges.jpg" title="orange" alt="orange" />';
        //echo $picture[$id];
        break;      
    default:
        $picture[$id] = '';
        echo $row['icon'] . " is something other than 1 2 or 3";
        break;
    } 
}
?>
<hr>
<?php 
echo $row0['monthday'];
echo "<br>";
echo $row0['title'];
echo "<br>";
echo $row0['message'];
echo "<br>";
echo $clean_sentence_row1;
echo "<br>";
echo $picture['1'];
echo "<br>";
?>
-----
<?php 
echo "<br>";
echo $row1['monthday'];
echo "<br>";
echo $row1['title'];
echo "<br>";
echo $row1['message'];
echo "<br>";
echo $clean_sentence_row2;
echo "<br>";
echo $picture['2'];
echo "<br>";
?>
-----
<?php 
echo "<br>";
echo $row2['monthday'];
echo "<br>";
echo $row2['title'];
echo "<br>";
echo $row2['message'];
echo "<br>";
echo $clean_sentence_row3;
echo "<br>";
echo $picture['3'];
echo "<br>";
?>
</body>
</html>
<hr>

 

 

Is there a way to simplify the code so its shorter and better but works, functions and looks the same.

 

Id be happy with some help.

 

 

Link to comment
https://forums.phpfreaks.com/topic/226421-simplifying-code-so-it-not-repeatable/
Share on other sites

I don't think I changed anything.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Example Form</title>
  <link rel="stylesheet" type="text/css" href="dd.css" />
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.dd.js"></script>
</head>
<body>


<!--
.style1 {
   font-family: Verdana, Arial, Helvetica, sans-serif;
   font-size: 12px;
   color: #000000;
   background-color: #FFFFCC;
   padding: 2px;
   height: 14px;
   width: 200px;
   border: 1px solid #7F9DB9;
}
--> 


<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
mysql_connect("localhost", "", "")or die("cannot connect");
mysql_select_db("test")or die("cannot select DB");
$tbl_name="test_mysql";
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

?>    
<br>
<?php
//$picture = array();
while($row = mysql_fetch_assoc($result))
{
$id = $row['id'];
switch ($row['icon']) {
    case 1:
        $picture[$id] = '<img src="http://www.nikibone.com/recipe/fruit/graphics/apples.jpg" title="apple" alt="apple" />';
        //echo $picture[$id];
        break;      
    case 2:
        $picture[$id] = '<img src="http://www.nikibone.com/recipe/fruit/graphics/bananas.jpg" title="banana" alt="banana" />';
        //echo $picture[$id];
        break;    
    case 3:
        $picture[$id] = '<img src="http://www.nikibone.com/recipe/fruit/graphics/oranges.jpg" title="orange" alt="orange" />';
        //echo $picture[$id];
        break;      
    default:
        $picture[$id] = '';
        echo $row['icon'] . " is something other than 1 2 or 3";
        break;
    } 
}

//FIRST ROW SHORTEN PARAGRAPH
$result0 = mysql_query("SELECT * FROM test_mysql LIMIT 0,3")or die(mysql_error());  
$row0 = mysql_fetch_array($result0);
while($row0 = mysql_fetch_assoc($result0)) {
$paragraph0 =  $row0['message'];
$rough_short_par0 = substr($paragraph0, 0, 100);
$clean_short_par0 = substr($rough_short_par0, 0);
$clean_sentence_row1 = $clean_short_par0 . "...";
?>
<hr>
<?php 
echo ($i > 0) ? '----' : NULL;
echo $row0['monthday']
	."<br>"
	.$row0['title']
	."<br>"
	.$row0['message']
	."<br>"
	.$clean_sentence_row1
	."<br>"
	.$picture[++$i]
	."<br>";

}
?>
</body>
</html>


How does these new rows solve your picture problem.  Your 3 rows selects pictures 1 - 3.

 

Short story is your limit.

 

LIMIT row_start , rows_returned.

 

So it would be:

LIMIT 0,6 //start at the first row, and return 6 of them.

 

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.