PHP - Basic Mvc Issue
I am quite new to php and the mvc setup, I am developing a library app however starting at the very basics so as not to become overwhelmed!
I am trying to do a basic insert to my book table, this is the code I have so far alsong with the error I am presented with.
Model (models > adminarea_model.php)
adminarea_model.php
public function create($title_text) { $title_text = strip_tags($title_text); $sql = "INSERT INTO book (title) VALUES (:title)"; $query = $this->db->prepare($sql); $query->execute(array(':title' => $title_text)); $count = $query->rowCount(); if ($count == 1) { return true; } else { $_SESSION["feedback_negative"][] = FEEDBACK_NOTE_CREATION_FAILED; } return false; } Similar TutorialsI think this is a simple question, but I'm just starting out and can't figure out how to do this exactly. I'm making a demo site for a flower shop. The structure is as such (only listing the particular files in question - once this is done, the rest will be easy) index.php -> inc_show_colors.php -> showColors.php The index is to be standard through the site, loading include files into a main content area. Clicking on a link loads inc_by_color.php (index.php -> index.php?page=by_color ) From there, one of the four colors is selected. showColors.php contains switch statements for each color. I want to load showColors.php into the main content window when a color is selected, and I need to have it work for all four colors. I'm not sure how to do this, as you can't pass a variable to an include from what I understand. What's the easiest way to go about this? Keep in mind this is an intro class and "best practices" are not necessary yet, just the simplest solution to get it working. I want each of these colors to link to another include file, which will load in its place in index.php. index.php: Code: [Select] <div id="maincontent"> <?php if (isset($_GET['page'])) { switch ($_GET['page']) { case 'by_color': include('includes/inc_by_color.php'); break; case 'by_type': include('includes/inc_by_type.php'); break; case 'by_occasion': include('includes/inc_by_occasion.php'); break; case 'view_cart': include('includes/inc_view_cart.php'); break; case 'payment_options': include('includes/inc_payment_options.php'); break; case 'home_page': default: include('includes/inc_home.php'); break; } } else include('includes/inc_home.php'); ?> </div> inc_by_color.php: The first link I tried doing in the way I thought would work, but it doesn't. It can't find the include file. The second one works, kind of - but it just loads the page by itself, not into the maincontent div in index.php. Code: [Select] <h1>Flowers Categorized by Color</h1> <div class="picdivcontainer"> <div class="picdiv"><a href="index.php?page=red"><img src="images/image.png" width="90" height="90" alt="Red Flowers" /> <p>Red</a></p></div> <div class="picdiv"><a href="showColors.php?color=white"><img src="images/image.png" width="90" height="90" alt="White Flowers" /></a> <p><a href="showColors.php?color=white">White</a></p></div> </div> and showColors.php: Code: [Select] if (isset($_GET['color'])) { switch ($_GET['color']) { case 'red': $result = mysql_query("SELECT * FROM flowers WHERE color='red'") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { // Set and trim strings and show the resulting flowers from query $flowername = $row['name']; $flowerimage = $row['image']; $image_path = "images/"; $flowername = str_replace('_', ' ', $flowername); $flowername = ucwords($flowername); ?> <div class="picdiv"><p><?php echo $flowername."<br />"; $flowerimage = str_replace(' ', '_', $flowerimage); echo "<img src='images/" . $row['image'] . "' />"; ?></p></div> <?php } break; I realize that showColors.php is probably a really messy way of doing this, but I just need to get it functioning ASAP for now. I'm trying to create a website so that if I go to http://websiteaddress.com/, it will go to then index and if you are already logged in it will display the main page, but if you aren't logged in it'll show a login/registration page. Also, how does the following work: index.php?action=home index.php?action=register They show two seperate pages? So, could it go to the home page if logged in, but if not logged in it'll go the registration/login page? I have 3 tables (sectors, subsectors, and business). From the site a site user can select a sector, which then lists the subsectors, then when they select a sub sector they can view a list of the businesses within the selection. I am trying to remove a step so that they will see the sectors and be able to select, then will list the subsectors with the business names under the subsectors. The code I have is prior to removing the step is ...... Code: [Select] <?php $rs=mysql_query("select * from tblmain WHERE id='$view'"); while($row=mysql_fetch_row($rs)){ foreach ($row as $k => $v){ $row[$k] = nl2br($v); } echo("<h4>" . $row[1] . " Sectors </h4>"); } ?> <ul> <?php $pnds=1; if (isset($_GET["id"])) $pnds=$_GET["id"]; $sql = "select * from tblsub WHERE catid='$view' ORDER BY subsec "; $reccount=$sq->numsrow($sql); if ($reccount > 0) { $ata =$sq->query($sql); while($rs=$sq->fetch($ata)) { ?> <li><a href="listings.php?view=<?php echo $rs["subsec"];?>"><?php echo $rs["subsec"];?></a></li> <?php } } ?> </ul> I have tried to get this to work but the problem I am having is $view is an number but the actual business table is not id, it is by subsector name. I have tried ....... Code: [Select] <?php $rs=mysql_query("select * from tblmain WHERE id='$view'"); while($row=mysql_fetch_row($rs)){ foreach ($row as $k => $v){ $row[$k] = nl2br($v); } echo("<h4>" . $row[1] . " Sectors </h4>"); } ?> <ul> <?php $pnds=1; if (isset($_GET["id"])) $pnds=$_GET["id"]; $sql = "select * from tblsub WHERE catid='$view' ORDER BY subsec "; $reccount=$sq->numsrow($sql); if ($reccount > 0) { $ata =$sq->query($sql); while($rs=$sq->fetch($ata)) { ?> <li><a href="listings.php?view=<?php echo $rs["subsec"];?>"><?php echo $rs["subsec"];?></a></li> <?php $rs=mysql_query("select * from tblbusiness WHERE category='$view' ORDER BY name"); while($row=mysql_fetch_row($rs)){ foreach ($row as $k => $v){ $row[$k] = nl2br($v); } print(" . $row[2] . "); } <?php } } ?> </ul> But obviously category is by name and not $view which is an id. Any help would be greatly appreciated. Does it matter where you start your session at? Does it have to be before the <!doctype> tag or can it go anywhere in the page? Hello,
I hope I picked the right category.
I have an interesting situation. I'm building a website and I want to have the ability to work on it / prototype it without having the website accessible to the public. Currently it's not accessible because I placed a .htaccess file into my server but I think it didn't work correctly because I can't actually log in. That's not the main problem, I think the problem is that it's not ASCII but UTF-8. Been trying to figure out how to convert that.
I'm not sure if I even need web but I figure if I'm going to test it, which one of the things I have to test is capturing video with my phone then storing it to my server then retrieving it again. The capturing should be easy through HTML5 from what I've read.
I have access to php and sql. I just don't know if I need internet access. I don't know how to store locally.
Most php scripts don't even seem to mention a storage location. I have set up sql databases that used php to gather information. It's just not fresh in the mind at the moment.
I mean it had to work right? I mean I get the pop up that says "Authentication Required" which wasn't there until I uploaded the .htaccess file, which how can the .htpasswd file not be read if the .htaccess file is being read. I think it is in the right folder... anyway, maybe I don't need the web to prototype my website
Except for the phone part, I would like to see that the phone's camera UI and camcorder UI is easily accessed by one button
Any help would be appreciated
I'm working with the youtube API and the function Code: [Select] $relatedVideo->watchURL returns a video url However when I use Code: [Select] preg_replace("youtube","test",$relatedVideo->watchURL); it gives me the following error Code: [Select] preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Delimiter must not be alphanumeric or backslash in <b>C:\wamp\www\site\test.php</b> on line <b>317 Help please? is it okay to name a column "date" in a table ??? just want to know whether this could mess up everything thanks in advance This is what I want:
If comparisonTimeH isn't zero, I want it to check if comparisonTimeH is zero, and comparisonTimeM is larger than or equal to 2. If so, I want it to pass and run the script.
This is what I have, and it's not right. It runs the else statement, echo test 2.
if ($comparisonTimeH != 0 || $comparisonTimeH = 0 AND $comparisonTimeM >= 1) { echo "test"; } else { echo "test2"; } echo "<br />" . $comparisonTimeH . " " . $comparisonTimeM;My last echo, the one outside of the if statements - it returns 0 and 12. Thus, both conditions that I'm trying to get, are true. My if's logic is clearly wrong, and I can't figure out what operators to use. If I change the AND to &&, it breaks and my final echo doesn't return comparisonTimeH (it's blank). Please teach me, thanks! Edited by icor1031, 17 September 2014 - 07:48 PM. Hello, Im from the Netherlands and i have a little question about php. I have a form in php where administrators can make new users. They can fill in a name, password, age and can select a role. The role is a dropdown menu where they can select athlete, researcher and trainer. My problem is that when they select a role, i want that selected role transfert to a php variable. I want to use the variable in a if statment, when sporter selected a new dropdown appears with options like tennis, hockey.... I have seen a lot on google about this with jquery and stuff, but i cant find a good example. Can u help me? Thanks. I would like to keep all my classes in a folder placed within the htdocs folder where my php script runs. Let's say the classes folder is com\jimslounge\classes. From my script can I instantiate a class like this $myClass = new com\jimslounge\classes\Jimsclass(); Does it work something like that? I would hate to have to use include to access classes. <?php include "C:/php-login/ASEngine/AS.php";
if (! app('login')->isLoggedIn()) {
This php code is in a file called test.php and is on my drive Letter O in a sub directory called my old photos . I'm having a problems referencing the include back to the the path of where ASEngine/As.php which is on c drive . I have tried all different relative and absolute paths . Any help would be appreciated Thank You Quote from: http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-1.php getter and setter names should match the property names. This way, when other PHP programmers want to use your objects, they will know that if you have a method/function called 'set_name()', there will be a property/variable called 'name'. Is this an accurate statement? Even if there's like 20+ properties? That would be 40+ methods and that's not including doing anything with those properties but setting/getting. That sounds...I don't know...against the grain. Hi, can you help me please? I have some code written about 5 yrs ago that is for example like this echo"<div class=\"select\">"; echo"<br /><br />\n"; echo"</div>\n"; echo"</div>\n"; echo "<p><b>$address</b></p>\n"; Does it make any difference at all if I amended it to echo"<div class=\"select\"><br /><br /></div></div><p><b>$address</b></p>"; ie coding errors or increase load time? also when should I use \n"; or "; Please forgive my ignorance but I am a self taught php writer and have managed to teach myself but just need a little help. Cheers Hello all. I'm learning Object oriented php and found this simple piece of code online but i don't understand why the variable $con must be returned: Code: [Select] class Connect { public static function con() { $con=mysql_connect("localhost","root","");// Connects to DB??? mysql_query("SET NAMES 'utf8'"); // Sets charset to UTF8 mysql_select_db("blog"); // Selects the DB return $con; // Don't know why $con must be returned, but it wont work without this. } } $con is later used in the script like this from another class to run a Query: Code: [Select] mysql_query($somequery,Connect::con()); Thanks for your help. I am creating a library app for personal development and would like to further my knowledge of MySql.
I currently have two tables 'book' and 'category'
book
id title author category isbn ---- ------- ---------- ---------- ------- 1 Treasure Chest Jim Jones 1 14252637 2 Pirates Boat Sue Smith 2 88447737 3 Adventure Land Harry Jo 3 01918273 4 Winter Week Sam Dill 3 00999337 category id cat_name ---- ------- 1 Horror 2 Kids 3 Fiction 4 Science I am doing a simple search where I want to display all books (select * from book) and I would like to display all the books with their corresponding categories. This works but displays the category number instead of the category name. How can I alter these tables in such a way that the cat_name and category are joined? I tried to run a command in phpmyadmin as below however it returned an error; ALTER TABLE book ADD FOREIGN KEY (category) REFERENCES category(cat_name);error #1452 - Cannot add or update a child row: a foreign key constraint fails (`sslib`.`#sql-1ab4_1dae`, CONSTRAINT `#sql-1ab4_1dae_ibfk_1` FOREIGN KEY (`category`) REFERENCES `category` (`cat_name`)) Hi all, I have been looking at thumbnail scripts and they are all very advanced for what I want. I just want to take 3 uploadeed jpg images, save a copy in original format and save a copy in images/thumbs/ with a size of 200px x 133px. Could someone just show me who to do this? Thanks The script I have for uploading the actual image is: <?php $destination='aircraft/'.$reg."1.jpg"; $temp_file = $_FILES['image']['tmp_name']; move_uploaded_file($temp_file,$destination); $destination2='aircraft/'.$reg."2.jpg"; $temp_file2 = $_FILES['image2']['tmp_name']; move_uploaded_file($temp_file2,$destination2); $destination3='aircraft/'.$reg."3.jpg"; $temp_file3 = $_FILES['image3']['tmp_name']; move_uploaded_file($temp_file3,$destination3); ?> for timthumb in my wordpress i use this code to function first_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $first_img = $matches [1] [0]; if(empty($first_img)){ //Defines a default image $first_img = "images/default.gif"; } return $first_img; } it works really well, then i thought why not to make it show random images if timthumb dont find any image? so the 1st googling i did came up with this function: <?php function getRandomFromArray($ar) { mt_srand( (double)microtime() * 1000000 ); $num = array_rand($ar); return $ar[$num]; } function getImagesFromDir($path) { $images = array(); if ( $img_dir = @opendir($path) ) { while ( false !== ($img_file = readdir($img_dir)) ) { // checks for gif, jpg, png if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) { $images[] = $img_file; } } closedir($img_dir); } return $images; } $root = ''; // If images not in sub directory of current directory specify root //$root = $_SERVER['DOCUMENT_ROOT']; $path = 'images/'; // Obtain list of images from directory $imgList = getImagesFromDir($root . $path); $img = getRandomFromArray($imgList); ?> and to show random image i Place the following where i wish the random image to appear: Code: [Select] <img src="<?php echo $path . $img ?>" alt="" /> my question is how to place this code in my function first_image?? i need to do something like this, but it doesnt make any sense: if(empty($first_img)){ //Defines a default image $first_img = " <img src="<?php echo $path . $img ?>" alt="" /> this was my thought, if i can mix those 2 normally it should work, right? or maybe there is another way to show random image in the Code: [Select] [i] if(empty($first_img)){ //Defines a default image[/i] [b] $first_img = ? Hey Guys Im following PHPACADEMY tutorial for integrating login. Im on the second video The problem im getting is when i enter in my login and password from my table in my database [(alex, abc) I tried copying them directly also] "I get the error that user doesn't exist" index.php Code: [Select] <html> <form action='login.php' method='POST'> Username: <input type='text' name='username'><br> Password: <input type='password' name='password'><br> <input type='submit' value='Log In'> </form> </html> Login.php Code: [Select] <?php $username = $_POST['username']; $password = $_POST['password']; if ($username&&$password) { $connect = mysql_connect("localhost","root","") or die ("couldnt connect"); mysql_select_db("phplogin") or die ("could not connect to db"); $query = mysql_query("SELECT * FROM users WHERE username='$username'"); $numrow = mysql_num_rows($query); if ($numrows!=0) { //code to login } else die("that user doesn't exist"); } else die("Please Enter username and password"); ?> Hello there. Well this problem maybe basic but is kind of annoying me. I havea solution to it which I wil lexplain. I have created a .php document which has html codes as well. It is a form that provides validation, for arguments sake lets say it is a form that validates if a user has entered their username and password, if not it will produce an error. The problem I am having is I have embedded the code inside the echo " code is here "; Now I have looked at escape characters and tried afew things, yet it still produces the same error on the same line. Example echo " !!!!!!!<form action='book.php' method='post' onsubmit="return checkForm('flogin');">!!!!!! The reset of the form is here ..... ""; echo The area highlighted in exclamation marks is where the area occurs, due to the quotes. Any suggestions. Thanks. |