Russia Posted February 2, 2011 Share Posted February 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226421-simplifying-code-so-it-not-repeatable/ Share on other sites More sharing options...
jcbones Posted February 2, 2011 Share Posted February 2, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/226421-simplifying-code-so-it-not-repeatable/#findComment-1168674 Share on other sites More sharing options...
Russia Posted February 2, 2011 Author Share Posted February 2, 2011 There is an error: Notice: Undefined variable: i in C:\wamp\www\update.php on line 74 Notice: Undefined variable: i in C:\wamp\www\update.php on line 83 Quote Link to comment https://forums.phpfreaks.com/topic/226421-simplifying-code-so-it-not-repeatable/#findComment-1168675 Share on other sites More sharing options...
jcbones Posted February 2, 2011 Share Posted February 2, 2011 Yep, I didn't take anything out, but I did add something in Just forgot to declare a variable. add it in: //FIRST ROW SHORTEN PARAGRAPH $i = 0; Sorry. Quote Link to comment https://forums.phpfreaks.com/topic/226421-simplifying-code-so-it-not-repeatable/#findComment-1168677 Share on other sites More sharing options...
Russia Posted February 2, 2011 Author Share Posted February 2, 2011 Okay, also, why is it only shows 2 rows? I have about 6 rows, I need it to show all the rows... How would I do that? Quote Link to comment https://forums.phpfreaks.com/topic/226421-simplifying-code-so-it-not-repeatable/#findComment-1168680 Share on other sites More sharing options...
jcbones Posted February 2, 2011 Share Posted February 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226421-simplifying-code-so-it-not-repeatable/#findComment-1168681 Share on other sites More sharing options...
Russia Posted February 2, 2011 Author Share Posted February 2, 2011 for some reason it skips the first row... It shows rows 2-6... basically I only see 5 icons and texts instead of 6... Why does it do that? Quote Link to comment https://forums.phpfreaks.com/topic/226421-simplifying-code-so-it-not-repeatable/#findComment-1168682 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.