Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Mdst 3559-03-03-sql-php-2

1,543 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Mdst 3559-03-03-sql-php-2

  1. 1. SQL + PHP 2<br />MDST 3559: DataestheticsProf. Alvarado03/03/2011<br />
  2. 2. Business<br />Scholars’ Lab Talk today <br />"A Plea for Open Digital Humanities Work: Or, A DH Grad Student Reflects on Years of 'Study'"Forget all the talk about who’s a practicing digital humanist and who isn’t. Jeremy Boggs argues that instead of coming up with a canonical set of skills digital humanists should have, we should instead ensure that the tools we employ, and the projects we create, are open, documented, and learnable. Then, encourage people to tinker around.<br />
  3. 3. Review<br />How to talk to MySQL from within PHP<br />Basic Pattern<br />Use the Zend_Db library<br />Establish a connection with the database<br />Send the database a query<br />Get a 2D array back<br />Loop through the array and do stuff with the data<br />Look at the code …<br />
  4. 4. Overview<br />Today we are going to improve up the code and add interactivity<br />We will create a form that lets users query the database<br />
  5. 5. Getting Started<br />Create subdirectory 03-03<br />Copy file lesson.phpfrom previous lesson<br />
  6. 6. Exercise 1<br />Put db_creds.php in your home directory, below public_html<br />Put the database connection code in a separate file and include it<br />Create a file called db_connect.php<br />Cut the database connection code from lesson.php and put it in the new file<br />Add a require() statement to insert the code in your page<br />
  7. 7. Exercise 2<br />Create a real HTML page<br />Add HTML elements after the initial PHP block<br />Pattern: Handle data first, then handle display<br />Remove echo statements and replace with string concatenation<br />Place string in page <br />
  8. 8. Exercise 3<br />Create a form to add interactivity<br />Add a FORM element to the body:<br />Note use of ‘text’ for all fields<br /> <form action=''method='GET'><br /> Name: <input type='text' name='country_name'/><br /> Debt: <input type='text' name='debt'/><br /> Network: <input type='text' name='network'><br /> <input type='submit' value='Search'/><br /> </form><br />
  9. 9. Exercise 3<br />The form sends data to the page called in the ACTION attribute<br />In this case, the page calls itself<br />The page that is called must know that it has data being sent to it<br />PHP provides a built-in array to store this data<br />$_GET<br />
  10. 10. Query Strings<br />HTTP’s URL syntax allows for key/value pairs to be appended to the end of a URL<br />http://foo.com/bar.php?x=10&y=blue<br />Question mark (?) begins the query string<br />Keys and values separated by equal signs (=)<br />Pairs separated by ampersands (&)<br />Spaces represented by plus signs (+)<br />Query strings are like arguments for pages that have code (like PHP pages)<br />
  11. 11. FORMs<br />Forms take values entered by a user and sends them to the server<br />The METHOD attribute determines how this data is sent<br />If METHOD is set to GET, then the form creates a query string and sends it to the server<br />There are several form elements<br />OPTION, INPUT (type=text, radio, checkbox, submit) TEXTAREA<br />
  12. 12. Exercise 4<br />Do something interesting with the data, like wrap the country names in URLs that point to web services that have information about countries<br />Wikipedia<br />Geonames<br />
  13. 13. Assignment<br />Find a data source on the web with at least three columns of data<br />One column should be numeric<br />Refer to data sources described in the Data Journalism articles<br />Import the source into your database<br />See post “[11.1] Exercise 1” for guidance<br />In the 03-03 directory, create a PHP file (assignment.php) that presents the new table as HTML<br />You may use the TABLE, OL, or some other combo of HTML elements to display the data<br />Use CSS to format your page as desired<br />

×