omardavinci Posted September 10, 2016 Share Posted September 10, 2016 (edited) Hi i did one webpage in php with a different tables and posts. But i need help because i want to add in this page one type and one input in my table for searching in each column. CREATE TABLE IF NOT EXISTS `coffee` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `type` varchar(255) DEFAULT NULL, `price` double DEFAULT NULL, `roast` varchar(255) DEFAULT NULL, `country` varchar(255) DEFAULT NULL, `image` varchar(255) DEFAULT NULL, `review` text, `ide` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; -- -- Gegevens worden uitgevoerd voor tabel `coffee` -- INSERT INTO `coffee` (`id`, `name`, `type`, `price`, `roast`, `country`, `image`, `review`) VALUES (1, 'Cafe au Lait', 'Classic', 2.25, 'Medium', 'France', 'Images/Coffee/Cafe-Au-Lait.jpg', 'A coffee beverage consisting strong or bold coffee (sometimes espresso) mixed with scalded milk in approximately a 1:1 ratio.'')'), (2, 'Caffe Americano', 'Espresso', 3.25, 'Medium', 'Italy', 'Images/coffee/caffe_americano.jpg', 'Similar in strength and taste to American-style brewed coffee, there are subtle differences achieved by pulling a fresh shot of espresso for the beverage base.'), (3, 'Peppermint White Chocolate Mocha', 'Espresso', 3.25, 'Medium', 'Italy', 'Images/coffee/white-chocolate-peppermint-mocha.jpg', 'Espresso with white chocolate and peppermint flavored syrups and steamed milk. Topped with sweetened whipped cream and dark chocolate curls.'), (4, 'Galao', 'Latte', 4.2, 'Light', 'Portugal', 'Images/Coffee/galao_kaffee_portugal.jpg', 'Galao is a hot drink from Portugal made of espresso and foamed milk'); In model php contains database related code for page of table: //Get coffeeEntity objects from the database and return them in an array. function GetCoffeeByType($type) { require ('Credentials.php'); //Open connection and Select database. mysql_connect($host, $user, $passwd) or die(mysql_error); mysql_select_db($database); $query = "SELECT * FROM coffee WHERE type LIKE '$type'"; $result = mysql_query($query) or die(mysql_error()); $coffeeArray = array(); //Get data from database. while ($row = mysql_fetch_array($result)) { $id = $row[0]; $name = $row[1]; $type = $row[2]; $price = $row[3]; $roast = $row[4]; $country = $row[5]; $image = $row[6]; $review = $row[7]; $ide = $row[8]; //Create coffee objects and store them in an array. $coffee = new CoffeeEntity($id, $name, $type, $price, $roast, $country, $image, $review); array_push($coffeeArray, $coffee); } //Close connection and return result mysql_close(); return $coffeeArray; } And here the code that will connect this to my php: <?php $title = "Manage coffee objects"; include './Controller/CoffeeController.php'; $coffeeController = new CoffeeController(); $content = $coffeeController->CreateOverviewTable(); if(isset($_POST['types'])) { //Fill page with coffees of the selected type $coffeeTables = $coffeeController->CreateCoffeeTables($_POST['types']); } else { //Page is loaded for the first time, no type selected -> Fetch all types $coffeeTables = $coffeeController->CreateCoffeeTables('%'); } if(isset($_GET["delete"])) { $coffeeController->DeleteCoffee($_GET["delete"]); } include './Template.php'; ?> So please if you have some suggestion how to include in my php the type and input, and it uses to search in my table; say me. All sugestions, dissapointments are good received Edited September 10, 2016 by omardavinci Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 10, 2016 Share Posted September 10, 2016 (edited) 1 - stop using the MySQL_* functions. They are deprecated. You will be sorry if you continue to use this when you finally have to deal with a php version that has actually removed them. 2 - If you use something like pdo instead of MySQL_* you could simply return an object and avoid a lot of rather verbose code in your get function. 3 - What do you mean by "how to include in my php the type and input"? I see that you use a $type var in your query but I don't know what you want us to tell you. I do not see any mention of anything like 'input', so what is that? 4 - Your table defines the 'type' field as being rather large. Is that really necessary, especially since you will be doing searches on it? Something like that invites a lot of variations on what perhaps s/b a structured field having specific values that may be coded instead of being literal. PS - if doing a like query perhaps you want to say "select ... where type='" . $type . '%'. "'"; Or with PDO, use a prepared query in place of that. Check the manual please. Edited September 10, 2016 by ginerjm Quote Link to comment Share on other sites More sharing options...
omardavinci Posted September 10, 2016 Author Share Posted September 10, 2016 1. If i mustnt use function what do you recomend to me? 2.Are you refering to this? http://php.net/manual/es/ref.pdo-mysql.php 3.I want to include in the tables one input or type to search in the fields. 4. So you recommend me to do this query: "select ... where type='" . $type . '%'. "'";? About pdo i will check the manual, you said me it is deprecated Quote Link to comment Share on other sites More sharing options...
Barand Posted September 10, 2016 Share Posted September 10, 2016 The slowest part of your function is the database connection. Do not connect every time you call the function - coonect once only at the top of your script then pass the connection variable as a parameter to your functions. You also fetch each row as an array of columns, move them from the array to individual variables then create an object from those variables which you then add to an array. Sounds like a lot of work. Why not pass the row to create your coffee object? while ($row = mysqli_fetch_array($conn, $result)) { // if you use mysqli $coffeeArray[] = new CoffeeEntity($row); } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 10, 2016 Share Posted September 10, 2016 1 & 2 - Yes. 3 - ? 4 - Uh, yes? Still don't know what you are talking about as to "type or input". Are you talking about how to use an input form to get data from the user to do your search with? Quote Link to comment Share on other sites More sharing options...
omardavinci Posted September 10, 2016 Author Share Posted September 10, 2016 One question i changed( mysql_ for mysqli_ ) But anyways i get still errores like these: Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\wamp\www\CoffeeWebsite\Model\CoffeeModel.php on line 31 Call Stack Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\CoffeeWebsite\Model\CoffeeModel.php on line 34 Call Stack Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\wamp\www\CoffeeWebsite\Model\CoffeeModel.php on line 34 Call Stack Some suggestions? dissapointments are good received About input i will take one page of example: http://www.voidrealms.com/index.php?r=tutorial/index Something like this. I mean it that Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 10, 2016 Share Posted September 10, 2016 You cannot simply change the function names by adding an 'i' to them. You must read the manual and see how to use those functions and what their syntaxes are. Personally - I think PDO is much better. As for you input/type question - you will have to explain yourself. Quote Link to comment Share on other sites More sharing options...
omardavinci Posted September 10, 2016 Author Share Posted September 10, 2016 Ok so i need to change parameters and sintaxis. Ok i will check tutorial and i will do the updates Quote Link to comment Share on other sites More sharing options...
Barand Posted September 10, 2016 Share Posted September 10, 2016 Ok i will check tutorial ... It's the manual that you should be checking http://uk1.php.net/manual/en/mysqli.select-db.php Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 10, 2016 Share Posted September 10, 2016 Yes! As I said earlier and as Barand says again - USE THE MANUAL. It's online and readily available. And it is correct unlike perhaps some sites or tutorials. Quote Link to comment Share on other sites More sharing options...
omardavinci Posted September 10, 2016 Author Share Posted September 10, 2016 one question about functions. the functions are still used in PDO? in this tutorial she is using PDO? Or with PDO it is not necessary, thx again and regard Quote Link to comment Share on other sites More sharing options...
omardavinci Posted September 10, 2016 Author Share Posted September 10, 2016 all it is ok thx Quote Link to comment 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.