Showing posts with label learnprogramming. Show all posts
Showing posts with label learnprogramming. Show all posts

Sunday, June 18, 2017

I'm taking an Intro to Programming course and was looking for a little help rephrasing my assignment to me or explaining exactly what it is asking. learnprogramming

So this is my assignment "students will draw a hierarchy chart, flowchart and write pseudocode to represent the logic for a program for Arnie's Appliances. Design a program that prompts the user for a refrigerator model name and the interior height, width, and depth in inches. Calculate the refrigerator capacity in cubic feet by first multiplying the height, width, and depth to get cubic inches, and then dividing by 1728 (the number of cubic inches in a cubic foot). The program accepts model names continuously until "XXX" is entered. Use named constants where appropriate. Also use modules, including one that displays End of Job after the sentinel is entered for the model name." What does it mean "prompts the user for a refrigerator model name"? The entire thing seems very cluttered to me so if someone could more concisely say what I need to do that would be great. Thank you.



Submitted June 19, 2017 at 06:00AM by Abconetwothree4 http://ift.tt/2sIcDGH learnprogramming

Tuesday, March 8, 2016

I feel like whenever I have to type in my code from scratch, I forget everything I have learned. Suggestions on how to correct this? learnprogramming

Hey /r/learnprogramming

I started learning JavaScript about a week ago on CodeAcademy. They teach you things by giving you an already completed bit of code and then tell you to make changes to it to get a different result. I complete these tasks with relative ease. However, once you do that a few times, they give you a blank slate and ask you to type up your own code to complete a nearly identical objective to whatever they just had you complete when you edited the preexisting code.

It's at this point that I stare at the screen and feel like I learned absolutely nothing. I feel like I have no idea how the syntax should be set up, where the braces go, what should be capitalized, what needs to be in parenthesis and what doesn't, why things even need parenthesis in the first place! I could go on but I'm sure you get the point.

What am I doing wrong in the earlier stages of learning that is making the information not "stick" like I need it to?

Thank you.

Edit: Thank you for all the responses. The consensus that I'm seeing is in repetition among other helpful tips such as completing a hello world as the first task every time. I plan on trying many of the suggestions.

I hope this helped others struggling with the same issue as well. Good luck to all those other learners out there, and I'd like to share one final quote with all of you that keeps me inspired to learn programming:

"If you want to set off and go develop some grand new thing; you don't need millions of dollars of capitalization. You need enough pizza and Diet Coke to stick in your refrigerator, a cheap PC to work on, and the determination to go through with it." -John Carmack (from 'The Masters of Doom')



Submitted March 07, 2016 at 10:51PM by Rorcap27 http://ift.tt/21jatn8 learnprogramming

Sunday, May 24, 2015

[c#] Im looking for your best real world examples on when you use interfaces. learnprogramming

Alrighty so before moving on in learning i feel like taking some more time on interfaces to see some actual use for them. Right now I understand how to use them but the why to use them is still kinda blurry to me. I saw a photo that helped on stackexchange that basically showed a bunch of appliances saying they are all classes ( refrigerator, toaster, microwave, etc) which all use the Interface IpowerPlug in order to work with class Power outlet. Could someone put that into code that I can have a look at I'd love to see an example of these classes and interfaces working in unison. Thanks in advance.



Submitted May 25, 2015 at 05:54AM by alixious http://ift.tt/1LA6PPj learnprogramming

Friday, September 26, 2014

Trouble with SELECT statement (PHP and MySQL) learnprogramming


Hello everyone, I am working on a practice program to help me understand how PHP and MySQL works. I am writing a program that works like a shopping website, one that contains the code, description, price, and user a product.


I want it to work like this:



Search: computer Results for computer 1 Brand New Computer 300.00 usergood 2 supercomputer 11.00 userbad 3 Busted Computer from the 80's 1500.00 userworst

However, when I run the code, I get the following:



Results for computer id name cost seller_id bids 1 1 Brand New iMac Computer Brand New iMac Computer 149.99 149.99 32 32 3 3

Not only do I get only one result, only one attribute is displayed per line.... twice.


What am I doing wrong?


Thanks!


dbsession.php



<?php /** * @staticvar MySql instance * * @return new MySql connection * Example usage: * * $MysqlObject = MySqlSingleton::getInstance(); * $MysqlObject->prepare($statement); * $MysqlObject->execute(); */ class MySqlSingleton{ /** * @staticvar * * Fill these in with database credentials */ private static $_dbHost = "127.0.0.1"; private static $_dbName = "****"; private static $_dbUser = "root"; private static $_dbPass = "****"; private static $_conn = NULL; public static function getInstance(){ if (self::$_conn == NULL) { try{ self::$_conn = new PDO('mysql:host=' . self::$_dbHost . ';dbname=' . self::$_dbName, self::$_dbUser, self::$_dbPass); } catch (PDOException $e) { //User friendly message echo '<p>Something went wrong.</p>'; /** * We should write this error to a log file */ require_once realpath(dirname(__FILE__) . '/..') . '<path to file>'; $error = 'Error: ' . $e->getMessage(); writeToMySqlLog($error); die(); } } return self::$_conn; } /** * Prevent __construct from being created */ protected function __construct(){ } /** * Prevent __clone from being created */ private function __clone(){ } /** * Prevent __wakeup from being called */ private function __wakeup(){ } } ?>

data.php, the file I want to access dbsession



<?php require_once("dbsession.php"); ?> <div id="container"> <div id="content"> <h4>Results</h4> <p>Two Results</p> <ul> <li>This</li> <li>That</li> </ul> </div> <div id="sidebar"> <h4>Search bar</h4> <?php if (isset($_REQUEST['search'])){ search(); } ?> <form> Search: <input type="text" name="input"/> <input type="submit" class = "button" name = "search" value = "Search"/> </form> <?php session_start(); function search(){ if(!empty($_REQUEST['input'])){ $input = $_REQUEST['input']; $db=MySqlSingleton::getInstance(); $pattern = '%'.$input.'%'; if(preg_match($pattern,$input)){ $query = $db->prepare("SELECT * FROM items WHERE name LIKE :names"); $query->execute(array('names'=>$pattern)); var_dump($query); $i=0; echo nl2br("<b>Results for $input</b> \r\n\n"); echo nl2br("id&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;&nbsp;&nbsp;&nbsp;cost&nbsp;&nbsp;&nbsp;&nbsp;seller_id&nbsp;&nbsp;&nbsp;&nbsp;bids \r\n"); $res = $query->fetch(); foreach($res as $key=>$value){ echo nl2br("$value \r\n"); $i++; } if(sizeof($res) == 1){ echo nl2br("\r\nSorry! No match :( \r\n"); } echo nl2br("\r\n<b>Reached the end of the search. Yielding $i results. </b>\r\n\n"); } } else{ echo nl2br("At least put something! >:( \r\n"); } } ?> </div> </div>

items.sql, the database I am using for the program



-- phpMyAdmin SQL Dump -- version 3.4.7.1 -- http://ift.tt/HXFLWR -- -- Host: localhost -- Generation Time: Jan 23, 2012 at 09:26 AM -- Server version: 5.1.56 -- PHP Version: 5.2.9 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -------------------------------------------------------- -- -- Table structure for table `items` -- CREATE TABLE IF NOT EXISTS `items` ( `id` int(11) NOT NULL, `name` varchar(65) COLLATE utf8_unicode_ci NOT NULL, `cost` float NOT NULL, `seller_id` int(11) NOT NULL, `bids` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- -- Dumping data for table `items` -- INSERT INTO `items` (`id`, `name`, `cost`, `seller_id`, `bids`) VALUES (1, 'Brand New iMac Computer', 149.99, 32, 3), (2, 'used diaper from my sister', 2.04, 1, 0), (3, 'Fresh apple pie', 14.99, 54, 32), (4, 'New gym socks', 2.34, 90, 566), (5, 'Weedwacker only slightly used', 4.56, 84, 2), (6, 'New ipad stolen from best buy', 399, 32, 23), (7, 'Book about having babies', 21.34, 44, 21), (8, 'Woman Jeans', 49.5, 56, 123), (9, 'traditional carpet', 25.45, 14, 75), (10, '3 boxes of frogs', 30.49, 68, 145), (11, '48 boxes of frogs', 74.29, 6, 99), (12, '7 boxes of frogs', 857.75, 18, 88), (13, 'laptop', 743.3, 89, 158), (14, 'thumbelina', 228.05, 15, 49), (15, 'bed', 127.15, 65, 189), (16, 'shampoing', 12.8, 6, 105), (17, 'stove', 37.66, 68, 111), (18, 'cushion', 7.15, 97, 157), (19, 'refrigerator', 657.49, 61, 129), (20, 'gold necklace', 853.07, 10, 101), (21, 'pan', 33.7, 7, 184), (22, 'awesome alien computer game', 10.75, 18, 29), (23, 'baby coat', 89.99, 14, 47), (24, 'baby seat', 145.78, 2, 199), (25, 'satchel', 44.71, 15, 66), (26, 'women perfum', 110.9, 48, 84), (27, 'conveyor belt', 1120.75, 11, 4), (28, 'used car', 5700.5, 12, 135), (29, 'supercomputer', 49.75, 50, 176), (30, 'mirror', 26.8, 19, 56), (31, 'piano', 1800.4, 13, 147), (32, 'quitar', 88.4, 25, 164), (33, 'trumpet', 255.15, 36, 23), (34, 'machintosh', 3845, 20, 107), (35, 'earphone', 10.5, 17, 110), (36, 'computer', 418, 11, 152), (37, 'night light', 13.87, 97, 198), (38, 'pc bag', 50.99, 48, 65), (39, 'babyfoot', 376.7, 2, 121), (40, 'hairdryer', 88.9, 12, 177), (41, 'babyliss', 130.75, 68, 79), (42, 'door', 150.5, 98, 13), (43, 'baby soap', 12.7, 4, 198), (44, 'used phone', 43.75, 9, 69), (45, 'bath', 757.15, 96, 55), (46, 'flower', 10.75, 16, 89), (47, 'battery charger', 48.75, 25, 87), (48, 'air conditioner', 975, 12, 151), (49, 'casserole', 115.75, 46, 35), (50, 'used toilet', 180.7, 64, 11), (51, 'teashirt', 14.98, 65, 114), (52, 'moto', 920, 22, 174), (53, 'saxophone', 220.9, 60, 140), (54, 'bicycle', 180.55, 97, 35), (55, 'man perfum', 95, 75, 199), (56, 'table', 157.25, 91, 48), (57, 'boat', 4890.5, 17, 177), (58, 'iphone', 547, 8, 28), (59, 'body milk', 50.5, 16, 90), (60, 'new curtain for bedroom', 278.4, 92, 11), (61, 'diamond ring', 1900, 15, 45), (62, 'swept', 4.5, 9, 99), (63, 'women hat', 17.55, 39, 60), (64, 'washing machine', 680.9, 42, 125), (65, 'baby bottle', 27.98, 91, 117), (66, 'women sun glasses', 66.7, 18, 174), (67, 'person weighs', 65.25, 10, 100), (68, 'photo frame', 18, 85, 170), (69, 'key board', 16.7, 90, 101), (70, 'screen', 250, 81, 188), (71, 'bucket', 2.5, 1, 19), (72, 'lipstick', 24.75, 3, 44), (73, 'wardrobe', 120.75, 9, 71), (74, 'blue dress size 40', 88.9, 7, 113), (75, 'newspaper', 1.5, 95, 172), (76, 'scanner', 350, 14, 62), (77, 'camera', 550.7, 17, 95), (78, 'camcorder', 788.99, 25, 127), (79, 'gun', 420.1, 81, 107), (80, 'domestic dog', 200, 19, 129), (81, 'horse', 759.5, 30, 115), (82, 'truck', 7800.5, 32, 123), (83, 'soccer ball', 95.49, 54, 155), (84, 'gold earring', 385, 75, 92), (85, 'basket', 250.45, 46, 142), (86, 'bikini', 85.2, 12, 57), (87, 'red skirt', 15.9, 18, 188), (88, 'copier machine', 800.7, 50, 160), (89, 'handbag', 35.9, 8, 108), (90, 'bath towel', 25.1, 11, 186), (91, 'coffee machine', 210.89, 15, 170), (92, 'wedding dress', 690, 26, 48), (93, 'man sun glasses', 80.7, 19, 174), (94, 'candle', 7.5, 22, 102), (95, 'scarf', 11.9, 7, 143), (96, 'microwave', 150.29, 6, 11), (97, 'electric oven', 645, 62, 171), (98, 'play station', 256.75, 12, 188), (99, 'dvd', 126.84, 14, 113), (100, 'magazine', 3.5, 8, 152); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;





Submitted September 26, 2014 at 09:25PM by betogm http://ift.tt/1v9EsRi learnprogramming

Tuesday, September 23, 2014

Difference between having a designated SQL connect file and calling database directly from the file needed (PHP) learnprogramming


Hi all,


I am practicing PHP and MySQL and have wondered the following:


What is the difference between having a designated class that connects to MySQL and having classes require to access it, ex:


dbsession.php



class dbsession{ function __construct(){ $db = new mysqli('dbhost','user','password','dbname'); } .... }

maindata.php



require('dbsession.php'); ....

insert.php



require('dbsession.php'); ....

and calling the database directly from the file itself, ex:


maindata.php



class maindata{ $db = new mysqli(blah,blah,blah,blah); //do what's needed here }

and the same on insert.php?


Thanks!


Code I want to use the db on



<?php require_once("dbsession.php"); ?> <div id="container"> <div id="content"> <h4>Results</h4> <p>Two Results</p> <ul> <li>This</li> <li>That</li> </ul> </div> <div id="sidebar"> <h4>Search bar</h4> <?php if (isset($_REQUEST['search'])){ search(); } ?> <form> Search: <input type="text" name="input"/> <input type="submit" class = "button" name = "search" value = "Search"/> </form> <?php session_start(); function search(){ if(!empty($_REQUEST['input'])){ $input = $_REQUEST['input']; $db=MySqlSingleton::getInstance(); $pattern = '%'.$input.'%'; if(preg_match($pattern,$input)==1){ $query = $db->prepare("SELECT * FROM items WHERE name LIKE ':name'"); $query->execute(array(':name'=>$pattern)); if($result = $query){ var_dump($result); $i=0; echo nl2br("<b>Results for $input</b> \r\n\n"); echo nl2br("id&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;&nbsp;&nbsp;&nbsp;cost&nbsp;&nbsp;&nbsp;&nbsp;seller_id&nbsp;&nbsp;&nbsp;&nbsp;bids \r\n"); $res = $query->fetchAll(PDO::FETCH_ASSOC);//return associative array foreach($res as $value){ //if(is_array($value)){ foreach($value as $key){ echo $key; } //} } /*foreach($result as $key=>$value){ if(is_array($value)){ foreach($value as $iKey=>$iValue){ echo $iValue['id'].' '. $iValue['name'].' '. $iValue['cost'].' '. $iValue['seller_id'].' '. $iValue['bids'].'<br/>'; $i++; } } }*/ } if(sizeof($result) == 1){ echo nl2br("\r\nSorry! No match :( \r\n"); } echo nl2br("\r\n<b>Reached the end of the search. Yielding $i results. </b>\r\n\n"); } } else{ echo nl2br("At least put something! >:( \r\n"); } } ?> </div> </div>

dbsession.php:



<?php /** * @staticvar MySql instance * * @return new MySql connection * Example usage: * * $MysqlObject = MySqlSingleton::getInstance(); * $MysqlObject->prepare($statement); * $MysqlObject->execute(); */ class MySqlSingleton{ /** * @staticvar * * Fill these in with database credentials */ private static $_dbHost = "127.0.0.1"; private static $_dbName = "****"; private static $_dbUser = "root"; private static $_dbPass = "****"; private static $_conn = NULL; public static function getInstance(){ if (self::$_conn == NULL) { try{ self::$_conn = new PDO('mysql:host=' . self::$_dbHost . ';dbname=' . self::$_dbName, self::$_dbUser, self::$_dbPass); } catch (PDOException $e) { //User friendly message echo '<p>Something went wrong.</p>'; /** * We should write this error to a log file */ require_once realpath(dirname(__FILE__) . '/..') . '<path to file>'; $error = 'Error: ' . $e->getMessage(); writeToMySqlLog($error); die(); } } return self::$_conn; } /** * Prevent __construct from being created */ protected function __construct(){ } /** * Prevent __clone from being created */ private function __clone(){ } /** * Prevent __wakeup from being called */ private function __wakeup(){ } } ?>

Database I am using



-- Host: localhost -- Generation Time: Jan 23, 2012 at 09:26 AM -- Server version: 5.1.56 -- PHP Version: 5.2.9 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `julio' -- -- -------------------------------------------------------- -- -- Table structure for table `items` -- CREATE TABLE IF NOT EXISTS `items` ( `id` int(11) NOT NULL, `name` varchar(65) COLLATE utf8_unicode_ci NOT NULL, `cost` float NOT NULL, `seller_id` int(11) NOT NULL, `bids` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- -- Dumping data for table `items` -- INSERT INTO `items` (`id`, `name`, `cost`, `seller_id`, `bids`) VALUES (1, 'Brand New iMac Computer', 149.99, 32, 3), (2, 'used diaper from my sister', 2.04, 1, 0), (3, 'Fresh apple pie', 14.99, 54, 32), (4, 'New gym socks', 2.34, 90, 566), (5, 'Weedwacker only slightly used', 4.56, 84, 2), (6, 'New ipad stolen from best buy', 399, 32, 23), (7, 'Book about having babies', 21.34, 44, 21), (8, 'Woman Jeans', 49.5, 56, 123), (9, 'traditional carpet', 25.45, 14, 75), (10, '3 boxes of frogs', 30.49, 68, 145), (11, '48 boxes of frogs', 74.29, 6, 99), (12, '7 boxes of frogs', 857.75, 18, 88), (13, 'laptop', 743.3, 89, 158), (14, 'thumbelina', 228.05, 15, 49), (15, 'bed', 127.15, 65, 189), (16, 'shampoing', 12.8, 6, 105), (17, 'stove', 37.66, 68, 111), (18, 'cushion', 7.15, 97, 157), (19, 'refrigerator', 657.49, 61, 129), (20, 'gold necklace', 853.07, 10, 101), (21, 'pan', 33.7, 7, 184), (22, 'awesome alien computer game', 10.75, 18, 29), (23, 'baby coat', 89.99, 14, 47), (24, 'baby seat', 145.78, 2, 199), (25, 'satchel', 44.71, 15, 66), (26, 'women perfum', 110.9, 48, 84), (27, 'conveyor belt', 1120.75, 11, 4), (28, 'used car', 5700.5, 12, 135), (29, 'supercomputer', 49.75, 50, 176), (30, 'mirror', 26.8, 19, 56), (31, 'piano', 1800.4, 13, 147), (32, 'quitar', 88.4, 25, 164), (33, 'trumpet', 255.15, 36, 23), (34, 'machintosh', 3845, 20, 107), (35, 'earphone', 10.5, 17, 110), (36, 'computer', 418, 11, 152), (37, 'night light', 13.87, 97, 198), (38, 'pc bag', 50.99, 48, 65), (39, 'babyfoot', 376.7, 2, 121), (40, 'hairdryer', 88.9, 12, 177), (41, 'babyliss', 130.75, 68, 79), (42, 'door', 150.5, 98, 13), (43, 'baby soap', 12.7, 4, 198), (44, 'used phone', 43.75, 9, 69), (45, 'bath', 757.15, 96, 55), (46, 'flower', 10.75, 16, 89), (47, 'battery charger', 48.75, 25, 87), (48, 'air conditioner', 975, 12, 151), (49, 'casserole', 115.75, 46, 35), (50, 'used toilet', 180.7, 64, 11), (51, 'teashirt', 14.98, 65, 114), (52, 'moto', 920, 22, 174), (53, 'saxophone', 220.9, 60, 140), (54, 'bicycle', 180.55, 97, 35), (55, 'man perfum', 95, 75, 199), (56, 'table', 157.25, 91, 48), (57, 'boat', 4890.5, 17, 177), (58, 'iphone', 547, 8, 28), (59, 'body milk', 50.5, 16, 90), (60, 'new curtain for bedroom', 278.4, 92, 11), (61, 'diamond ring', 1900, 15, 45), (62, 'swept', 4.5, 9, 99), (63, 'women hat', 17.55, 39, 60), (64, 'washing machine', 680.9, 42, 125), (65, 'baby bottle', 27.98, 91, 117), (66, 'women sun glasses', 66.7, 18, 174), (67, 'person weighs', 65.25, 10, 100), (68, 'photo frame', 18, 85, 170), (69, 'key board', 16.7, 90, 101), (70, 'screen', 250, 81, 188), (71, 'bucket', 2.5, 1, 19), (72, 'lipstick', 24.75, 3, 44), (73, 'wardrobe', 120.75, 9, 71), (74, 'blue dress size 40', 88.9, 7, 113), (75, 'newspaper', 1.5, 95, 172), (76, 'scanner', 350, 14, 62), (77, 'camera', 550.7, 17, 95), (78, 'camcorder', 788.99, 25, 127), (79, 'gun', 420.1, 81, 107), (80, 'domestic dog', 200, 19, 129), (81, 'horse', 759.5, 30, 115), (82, 'truck', 7800.5, 32, 123), (83, 'soccer ball', 95.49, 54, 155), (84, 'gold earring', 385, 75, 92), (85, 'basket', 250.45, 46, 142), (86, 'bikini', 85.2, 12, 57), (87, 'red skirt', 15.9, 18, 188), (88, 'copier machine', 800.7, 50, 160), (89, 'handbag', 35.9, 8, 108), (90, 'bath towel', 25.1, 11, 186), (91, 'coffee machine', 210.89, 15, 170), (92, 'wedding dress', 690, 26, 48), (93, 'man sun glasses', 80.7, 19, 174), (94, 'candle', 7.5, 22, 102), (95, 'scarf', 11.9, 7, 143), (96, 'microwave', 150.29, 6, 11), (97, 'electric oven', 645, 62, 171), (98, 'play station', 256.75, 12, 188), (99, 'dvd', 126.84, 14, 113), (100, 'magazine', 3.5, 8, 152); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;





Submitted September 23, 2014 at 08:17PM by betogm http://ift.tt/1rlzZK4 learnprogramming