Cell0518 Posted September 28, 2006 Share Posted September 28, 2006 Hi,I have a question. When coding a update form, I want the data to be pulled from a MySQL DB.In trying to code better and I'm looking to have a better layout for my code, so it seems less cluttered and more stuctured and possibly using a template.My current code is similar to this...[code]<?php$act = $_GET['act'];if($act == "") {$query = "GET title FROM table1";$db->query($query)while($row = mysqli_fetch_array($result, MYSQL_ASSOC)) { echo "<html>\n<head>....Update Data";echo "<input type=\"text\" value=\"".$row['title']."\">";echo "<input type=\"submit\" calue=\"\">";}}if($act == "update") {$query = ".....";if($db->query($query)) {$msg = "Successful" }else { $msg = "Unsuccessful" }echo "<html>\n<head>....Data Results..";echo $msg;echo "</body></html>";}?>[/code]Is there a better way to code something like this? (as in, separating the HTML from the results...)Thanks,Chris Quote Link to comment https://forums.phpfreaks.com/topic/22334-better-way-to-code-this/ Share on other sites More sharing options...
warewolfe Posted September 28, 2006 Share Posted September 28, 2006 Hej, I don't know if that is just a typo for the example butif($act = "") will always be true as it is true you are assigning $act the value of "" some people recommend doing an if check if("" == $act) {do something} because if you mistype and print if("" = $act) {do somthing} you'll find out pretty quickly.As for code layout, try bracketing and indenting in a consistent manner. All k&R or all Allman style. Mixing and matching makes for hard reading. Quote Link to comment https://forums.phpfreaks.com/topic/22334-better-way-to-code-this/#findComment-100033 Share on other sites More sharing options...
Cell0518 Posted September 28, 2006 Author Share Posted September 28, 2006 what is the difference? ( if ($act == "") and if ("" == $act) )Also, I don't want to sound stupid, but what is K&R and Allman?( I updated the original post :) )Thanks,Chris Quote Link to comment https://forums.phpfreaks.com/topic/22334-better-way-to-code-this/#findComment-100068 Share on other sites More sharing options...
Daniel0 Posted September 28, 2006 Share Posted September 28, 2006 Nothing... what is difference between 1+2=3 and 3=1+2? (nothing)Here is it rewritten: [code]<?phpif(empty($_GET['act'])){ $result = $db->query("GET title FROM table1"); while($row = mysqli_fetch_array($result, MYSQL_ASSOC)) { echo <<<EOF<html><head>....Update Data<input type="text" value="{$row['title']}" /><input type="submit" value="">EOF; }}else if($_GET['act'] == "update"){ $msg = $db->query(".....") ? "Sucessful" : "Unsucessful"; echo <<<EOF<html><head>....Data Results{$msg}</body></html>EOF;}?>[/code]You might want to consider using a switch instead. And you need to work on your HTML. Quote Link to comment https://forums.phpfreaks.com/topic/22334-better-way-to-code-this/#findComment-100072 Share on other sites More sharing options...
Cell0518 Posted September 28, 2006 Author Share Posted September 28, 2006 Hi, this was an example, simplified to not take up too much room. :) How does the <<<EOF .... EOF; work? (I'm taking it that anything within {} is parsed, everything else is not?)In the possiblity of using a switch, I assume the way to implement the pieces of code more effectively would be to include them from separate the files?My site would eventually be a news system (with image uploads and a WYSIWIG editor). If you know of any tutorials that would help, I would be greatly appreciated.Thanks,Chris Quote Link to comment https://forums.phpfreaks.com/topic/22334-better-way-to-code-this/#findComment-100077 Share on other sites More sharing options...
Flukey Posted September 28, 2006 Share Posted September 28, 2006 [quote author=Daniel0 link=topic=109789.msg442878#msg442878 date=1159423035]Nothing... what is difference between 1+2=3 and 3=1+2? (nothing)Here is it rewritten: [code]<?phpif(empty($_GET['act'])){ $result = $db->query("GET title FROM table1"); while($row = mysqli_fetch_array($result, MYSQL_ASSOC)) { echo <<<EOF<html><head>....Update Data<input type="text" value="{$row['title']}" /><input type="submit" value="">EOF; }}else if($_GET['act'] == "update"){ $msg = $db->query(".....") ? "Sucessful" : "Unsucessful"; echo <<<EOF<html><head>....Data Results{$msg}</body></html>EOF;}?>[/code]You might want to consider using a switch instead. And you need to work on your HTML.[/quote]You're forgetting about checking the input for SQL injection. Quote Link to comment https://forums.phpfreaks.com/topic/22334-better-way-to-code-this/#findComment-100125 Share on other sites More sharing options...
warewolfe Posted September 28, 2006 Share Posted September 28, 2006 Hej, The difference betweencase 1 ($var =="") and case 2 ("" == $var) is this, if you forget to add the second = sign in case 2, which happens often as your first unedited post shows, then the program will fail right away and let you know exactly where it fails and is very easy to debug. If you forget the second = sign in case 1 then the 'if' test will always return true, and sometimes your program will work and sometimes it won't. Much harder to debug. K&R is a style of code presentation (indenting and bracketing), so is Allman. I prefer Allman style as it is easier to match brackets but it doesn't really matter which one you do as long as you do one consistently.check out http://en.wikipedia.org/wiki/Indent_styleWW. Quote Link to comment https://forums.phpfreaks.com/topic/22334-better-way-to-code-this/#findComment-100146 Share on other sites More sharing options...
Daniel0 Posted September 28, 2006 Share Posted September 28, 2006 [quote author=Flukey link=topic=109789.msg442932#msg442932 date=1159435482]You're forgetting about checking the input for SQL injection.[/quote]No I didn't... you cant check for SQL injection as there is no variables in the query. Quote Link to comment https://forums.phpfreaks.com/topic/22334-better-way-to-code-this/#findComment-100237 Share on other sites More sharing options...
xyn Posted September 28, 2006 Share Posted September 28, 2006 This should work...[code=php:0]<?php$act = $_GET['act'];if( !$act ) {$query = mysql_query("SELECT title FROM table1");while($row = mysql_fetch_array($result, MYSQL_NUM)) { echo "<html>\n<head>....Update Data"; echo "<input type=\"text\" value=\"".$row['title']."\">"; echo "<input type=\"submit\" calue=\"\">";}} elseif($act == "update") {$query = ".....";if($db->query($query)){ $msg = "Successful";}else{ $msg = "Unsuccessful";}echo "<html>\n<head>....Data Results..";echo $msg;echo "</body></html>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22334-better-way-to-code-this/#findComment-100244 Share on other sites More sharing options...
Cell0518 Posted September 28, 2006 Author Share Posted September 28, 2006 Thank you for the information, especially showing the $msg = $db->query... line. This explains something I've wondered about for a while.As far as my site, it would eventually be a news system (with image uploads and a WYSIWIG editor). I'm looking at a modular design, similar to most forums, ie: example.com/?mod=news&act=update. How are you able to make a single php file that references all of the data passed by $_GET? My current page does most of this, but I'm looking to clean the code up....as a lot of the html is sprinkled in and part is just simply echoed.I'm looking at making each part of the news "CMS" modular, as you can see in the example URL, which is, as I said, similar to most news pages I've seen.. (ie * http://www.theinquirer.net/default.aspx?article=34724 * -> when default.aspx sees ?article=, it knows it is an article and it needs to lookup something in (a database?) that has the id of 34724, which is anything after ?article=.)I know that this specific forum uses semi-colons vs ampersands, but how is it all linked? What code is helping tell that when action=post, you need to get all of the required data, and give the end-user a text box to write in and when they "submit", it goes into a database?Also, if anyone has seen any tutorials, etc that may help answer this question, please post them. Again,I know I am asking a lot, but I feel that the best way for me to learn my php is by making something.Thanks,Chris Quote Link to comment https://forums.phpfreaks.com/topic/22334-better-way-to-code-this/#findComment-100248 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.