Joco Posted April 25, 2011 Share Posted April 25, 2011 Undefined variable: return line 38 that line is : echo $return; Below is a look at my full code. <?php class cms { var $host; var $username; var $password; var $db; function connect() { $con = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error()); mysql_select_db($this->db, $con) or die(mysql_error()); } function get_content($id = ''){ if($id !=""): $id = mysql_real_escape_string($id); $sql = "SELECT * FROM cms_content WHERE id = '$id'"; $return = '<a href="index.php">Go Back?</a>'; else: $sql = "SELECT * FROM cms_content ORDER BY id DESC"; endif; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) != 0): while($row = mysql_fetch_assoc($res)) { echo '<h1><a href="index.php?id=' . $row['id'] .'">' . $row['title'] .'</a></h1>'; echo '<p>' . $row['body'] . '</p>'; } else: echo '<p> Sorry! This Page doesn\'t exist!</p>'; endif; echo $return; } }//Ends our class ?> Quote Link to comment https://forums.phpfreaks.com/topic/234700-undefined-variable-return/ Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2011 Share Posted April 25, 2011 I don't see where $return is ever defined outside of a conditional, so if no conditional is TRUE, $return remains undefined. Quote Link to comment https://forums.phpfreaks.com/topic/234700-undefined-variable-return/#findComment-1206080 Share on other sites More sharing options...
Joco Posted April 25, 2011 Author Share Posted April 25, 2011 I'm lost on how to fix this problem any advice? Quote Link to comment https://forums.phpfreaks.com/topic/234700-undefined-variable-return/#findComment-1206087 Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2011 Share Posted April 25, 2011 You could move $return inside of the conditional in which it gets defined. Something tells me the code has other issues, though. Someone better versed in class definitions than I would need to make that call. Quote Link to comment https://forums.phpfreaks.com/topic/234700-undefined-variable-return/#findComment-1206102 Share on other sites More sharing options...
Joco Posted April 25, 2011 Author Share Posted April 25, 2011 Well the code works however the error is showing up on the index.php on line 24 if(isset($_GET['id'])): $obj->get_content($_GET['id']); else: $obj->get_content(); endif; line 24 for my code is: $obj->get_content(); Quote Link to comment https://forums.phpfreaks.com/topic/234700-undefined-variable-return/#findComment-1206106 Share on other sites More sharing options...
Joco Posted April 26, 2011 Author Share Posted April 26, 2011 Alright so sorry for the double posting i figured out another way that works pretty much the same way where i had the $return; i simply replace that with echo '<p><a href="index.php">Go Back?</a></p>'; and it works fine! Quote Link to comment https://forums.phpfreaks.com/topic/234700-undefined-variable-return/#findComment-1206220 Share on other sites More sharing options...
fugix Posted April 26, 2011 Share Posted April 26, 2011 label this as fixed please Quote Link to comment https://forums.phpfreaks.com/topic/234700-undefined-variable-return/#findComment-1206233 Share on other sites More sharing options...
Fadion Posted April 26, 2011 Share Posted April 26, 2011 I can't imagine an object oriented approach with a class named "cms". I suppose you're going to have a bunch of different flavored functions in there, with each one in it's completely separate logic. That's not programming in objects! It is just spaghetti code inside a class. Really, why bother with classes when you're not using them?! Dealing just with functions will be a lot easier in your case. Quote Link to comment https://forums.phpfreaks.com/topic/234700-undefined-variable-return/#findComment-1206238 Share on other sites More sharing options...
Joco Posted April 26, 2011 Author Share Posted April 26, 2011 I didn't make this script im only following along with a video i saw online im new to php so i just wanted to try to see if this CMS was going to be any good or not. i ran into another problem aswell it appears that in the index.php code it says Undefined index: add and i get that when i view the admin/index.php file. now the admin/index.php file has the following code in it. <?php if($_POST['add']): $obj->add_content($_POST); endif; ?> the error talks about [color=red]if($_POST['add']):[/color] that is basically inside the <form method="post" action="index.php"> [color=red]<input type="hidden" name="add" value="true" />[/color] <div> <label for="title">Title:</label> <input type="text" name="title" id="title" /> </div> <div> <label for="body">Body:</label> <textarea name="body" id="body" rows="8" cols="40"></textarea> </div> <input type="submit" name="submit" value="Add Content" /> </form> </div> I just wanted to try to see if i could following along with making a CMS that would be some what secure against website attacks like SQL Injections anyways the maker of the video sure in my view made this really bad. Quote Link to comment https://forums.phpfreaks.com/topic/234700-undefined-variable-return/#findComment-1206250 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.