Learning PHP and MySQL

Monday, August 07, 2006

Example 06-01 Using the array function to create an array of weekdays

<?php
$weekdays
=array('Monday',
                
'Tuesday',
                
'Wednesday',
                
'Thursday',
                
'Friday',
                
'Saturday',
                
'Sunday');
?>

Posted by krautgrrl on 08/07 at 10:17 AM
Chapter 6 Code • (0) Comments • (0) TrackbacksPermalink

Example 06-02 Creating an associative array of shapes

<?php
$shapes
=array('Soda Can'  =>  'Cylinder',
              
'Note Pad'  =>  'Rectangle',
              
'Apple'  =>  'Sphere',
              
'Orange'  =>  'Sphere',
              
'Phonebook'  =>  'Rectangle');
?>

Posted by krautgrrl on 08/07 at 10:18 AM
Chapter 6 Code • (0) Comments • (0) TrackbacksPermalink

Example 06-03 Displaying one value from an array

<?php
$shapes
=array('Soda Can'  =>  'Cylinder',
              
'Note Pad'  =>  'Rectangle',
              
'Apple'  =>  'Sphere',
              
'Orange'  =>  'Sphere',
              
'Phonebook'  =>  'Rectangle');
print  
"A  note  pad  is  a  {$shapes['Note Pad']}.";
?>

Posted by krautgrrl on 08/07 at 10:19 AM
Chapter 6 Code • (0) Comments • (2) TrackbacksPermalink

Example 06-04 Display the contents of an array using a loop

<?php
$shapes
=array('Soda Can'  =>  'Cylinder',
              
'Note Pad'  =>  'Rectangle',
              
'Apple'  =>  'Sphere',
              
'Orange'  =>  'Sphere',
              
'Phonebook'  =>  'Rectangle');
foreach (
$shapes  as  $key  =>  $value)  {
  
print"The $key is a $value.<br>\n";
}
?>

Posted by krautgrrl on 08/07 at 10:20 AM
Chapter 6 Code • (0) Comments • (1) TrackbacksPermalink

Example 06-05 Counting the elements in an array

<?php
$shapes
=array('Soda Can'  =>  'Cylinder',
              
'Note Pad'  =>  'Rectangle',
              
'Apple'  =>  'Sphere',
              
'Orange'  =>  'Sphere',
              
'Phonebook'  =>  'Rectangle');
$numElements  =  count($shapes);
print
"The array has $numElements elements.<br>\n";
?>

Posted by krautgrrl on 08/07 at 10:20 AM
Chapter 6 Code • (0) Comments • (0) TrackbacksPermalink

Example 06-06 Using sort to alphabetize

<?php
$shapes  
=  array("rectangle""cylinder""sphere");
sort($shapes);
//The  foreach  loop  selects  each  element  from  the  array  and  assigns  its  value  to  $key
//before  executing  the  code  in  the  block. foreach ($shapes  as  $key  =>  $val)  {
echo "shapes[" $key "]  =  " $val "<br>";
}
?>

Posted by krautgrrl on 08/07 at 10:21 AM
Chapter 6 Code • (1) Comments • (6) TrackbacksPermalink

Example 06-07 Creating a multidimensional array

<?php
$objects
=array('Soda Can' => array('Shape' => 'Cylinder',
                                    
'Color' =>  'Red',
                                    
'Material' => 'Metal'),
               
'Note Pad' => array('Shape' => 'Rectangle',
                                   
'Color' => 'White',
                                   
'Material' => 'Paper'),
               
'Apple' => array('Shape' => 'Sphere',
                                
'Color' => 'Red',
                                
'Material' => 'Fruit'),
               
'Orange' => array('Shape' => 'Sphere',
                                 
'Color' => 'Orange',
                                 
'Material'  =>  'Fruit'),
               
'Phonebook' => array('Shape' => 'Rectangle',
                                    
'Color' => 'Yellow',
                                    
'Material' => 'Paper'));
echo 
$objects['Soda Can']['Shape'];
?>

Posted by krautgrrl on 08/07 at 10:23 AM
Chapter 6 Code • (0) Comments • (1) TrackbacksPermalink

Example 06-08 Displaying a multidimensional array

<?php
foreach ($objects as $obj_key => $obj)
{
  
echo "$obj_key:<br>";
  while (list (
$key,$value)=each ($obj))
  
{
    
echo "$key = $value ";
  
}
  
echo "<br>";
}
?>

Posted by krautgrrl on 08/07 at 10:24 AM
Chapter 6 Code • (0) Comments • (3051) TrackbacksPermalink

Example 06-09 Using extract on an associative array

<?php
$shapes
=array('SodaCan'  =>  'Cylinder',
              
'NotePad'  =>  'Rectangle',
              
'Apple'  =>  'Sphere',
              
'Orange'  =>  'Sphere',
              
'PhoneBook'  =>  'Rectangle');

extract($shapes);
//  $SodaCan,  $NotePad,  $Apple,  $Orange,  and  $PhoneBook  are  now  set echo  $Apple;
echo "<br>";
echo 
$NotePad;
?>

Posted by krautgrrl on 08/07 at 10:24 AM
Chapter 6 Code • (0) Comments • (1) TrackbacksPermalink

Example 06-10 Using extract with the EXTR_PREFIX_ALL directive

<?php
$Apple
="Computer";
$shapes=array('SodaCan'  =>  'Cylinder',
              
'NotePad'  =>  'Rectangle',
              
'Apple'  =>  'Sphere',
              
'Orange'  =>  'Sphere',
              
'PhoneBook'  =>  'Rectangle');

extract($shapes,EXTR_PREFIX_ALL,"shapes");
//  $shapes_SodaCan,  $shapes_NotePad,  $shapes_Apple,  $shapes_Orange,  and
//$shapes_PhoneBook  are  now  set

echo "Apple is $Apple.<br>";
echo 
"Shapes_Apple is $shapes_Apple";
echo 
"<br>";
echo 
"Shapes_NotePad is $shapes_NotePad";
?>

Posted by krautgrrl on 08/07 at 10:25 AM
Chapter 6 Code • (0) Comments • (2) TrackbacksPermalink

Example 06-11 Using EXTR_PREFIX_ALL on a numeric array

<?php
$shapes
=array( 'Cylinder','Rectangle'); 
extract($shapes,EXTR_PREFIX_ALL,"shapes"); 
echo 
"Shapes_0 is $shapes_0 <br>";
echo 
"Shapes_1 is $shapes_1";
?>

Posted by krautgrrl on 08/07 at 10:25 AM
Chapter 6 Code • (0) Comments • (98) TrackbacksPermalink

Example 08-01 The contents of the my_backup.sql file

-- MySQL dump 10.9
--
-- 
Hostlocalhost    Databasetest
-- ------------------------------------------------------
-- 
Server version    4.1.11-Debian_4-log
--
-- 
Table structure for table `authors`
--
DROP TABLE IF EXISTS `authors`; 
CREATE TABLE `authors` ( `author_idint(11NOT NULL auto_increment,
             `
title_idint(11NOT NULL default '0',
             `
authorvarchar(125) default NULLPRIMARY KEY (`author_id`)
ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- 
Dumping data for table `authors`
--
/*!40000 ALTER TABLE `authors` DISABLE KEYS */LOCK TABLES `authorsWRITE;
INSERT INTO `authorsVALUES (1,1,'Ellen Siever'),(2,1,'Aaron Weber'),(3,2,'Arno ld Robbins'),(4,2,'Nelson Beebe');
UNLOCK TABLES;
/*!40000 ALTER TABLE `authors` ENABLE KEYS */;

Posted by krautgrrl on 08/07 at 05:10 PM
Chapter 8 Code • (3) Comments • (256) TrackbacksPermalink

Example 08-02 Book titles in CSV format

1,Linux in a Nutshell,476
2
,Classic Shell Scripting,256

Posted by krautgrrl on 08/07 at 05:10 PM
Chapter 8 Code • (1) Comments • (3) TrackbacksPermalink

Example 08-03 Creating a simple index

CREATE UNIQUE INDEX `authindON `authors` (`author`);

Posted by krautgrrl on 08/07 at 05:11 PM
Chapter 8 Code • (0) Comments • (0) TrackbacksPermalink

Example 08-04 Using CONCAT to put fields together

SELECT CONCAT(`title`,' has ',`pages`,' pages.'FROM `books`;

Posted by krautgrrl on 08/07 at 05:12 PM
Chapter 8 Code • (3) Comments • (0) TrackbacksPermalink
Page 1 of 3 pages  1 2 3 >

Statistics

This page has been viewed 374257 times
Page rendered in 0.3221 seconds
Total Entries: 224
Total Comments: 16
Total Trackbacks: 307338
Most Recent Entry: 09/27/2006 12:39 pm
Most Recent Comment on: 10/26/2007 10:00 am
Total Members: 2
Total Logged in members: 0
Total guests: 7
Total anonymous users: 0
Most Recent Visitor on: 02/06/2012 06:52 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm

Referrers

Powered by ExpressionEngine