Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/22334-better-way-to-code-this/
Share on other sites

Hej,
I don't know if that is just a typo for the example but

if($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.
Nothing... what is difference between 1+2=3 and 3=1+2? (nothing)

Here is it rewritten: [code]<?php
if(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.
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 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]<?php
if(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.
Hej,

  The difference between
case 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_style


WW.
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]
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
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.