Location: PHPKode > scripts > swemc > swemc/sample.php
<html>
<head>
  <title> swemc 1.0 Sample</title>
  <link rel="stylesheet" type="text/css" href="swemc.css">
  <meta name="Author" content="Uwe Stein">
 </head>
<body >
<h1 align="center">This sample-file shows how swemc works</h1>

<?php
   include_once("./swemc.php");

   // create the object
   $db = new swemc;

   // show mysql-errors if they occur
   $db->bDebug = TRUE;

   // change to your tablename
   $tablename = "sample_table";

   // build a querystring  -> we know, SELECT * should not be used
   //                      -> but is's only a sample   ;-)
   $query = "SELECT * FROM $tablename where 1";

   // execute the query
   $db->query($query);


   // show some output
   echo "<div align='center'>";
   echo "<br><br><h2>The function tab_out without CSS with table-head</h2>";
   // with thead, without CSS
   $db->tab_out(TRUE);

   echo "<br><br><h2>The function tab_out with CSS and table-head</h2>";
   // with thead and CSS
   $db->tab_out(TRUE,"swemc_table");

   echo "<br><br><h2>now with another CSS-class and table-head</h2>";
   // with thead and CSS
   $db->tab_out(TRUE,"swemc_table2");

   echo "<br><br><h2>the same CSS-class, without table-head</h2>";
   // with CSS, without thead
   $db->tab_out(FALSE,"swemc_table2");


   // now, lets try the function td_with
   // cause i dont know your "sample_table", i use percent
   // the result depends on the number of columns in your table
   $nf = $db->iFields; // the number of fields in your table

   $w_arr = array();  // keeps the width-values

   // we build the "width-array"
   for ( $i=0; $i < $nf; $i++)
   {
       if ( $i % 2)
	       $w_arr[] = "15%";
       else
	       $w_arr[] = "5%";
   }

   // define the <td> - width
   $db->td_width($w_arr);

   // and show the table
   echo "<br><br><h2>The table with different width's<br> with CSS without table-head</h2>";
   $db->tab_out(FALSE,"swemc_table");


   // print some  single records with function single_out in a table
   // left column without CSS, right column with CSS
   echo "<br><br><h2>Here you see the function single_out</h2>";
   echo "<table>";
   echo "<tr><td>without CSS</td><td>with CSS</td></tr>";

   // the number of records in your result
   $nr = $db->iRecs;

   // limit the counter to 5 or the number of records in your table
   $max = ( $nr < 5)? $nr : 5;
   // now we print the single records
   for ($i=0; $i < $max; $i++)
   {
        echo "<tr><td align='center'>";
        $db->single_out($i);
		echo "</td><td align='center'>";
		$db->single_out($i,"swemc_table");
		echo "</td></tr>";
   }
   echo "</table>";
   echo "</div>"; // end of div align="center"


   // the usage of fetch_field
   echo "<br><br><h2>the function fetch_field</h2>";

   $nf = $db->iFields; // the number of columns
   // limit the counter to 5 or the number of columns in your table
   $fmax = ( $nf < 10)? $nf : 10;
   // we fetch the data and show them
   for ( $i=0; $i < $max; $i++)  // $max as set above
   {
       echo "<br><br>record $i";
	   for ( $j=0; $j < $fmax; $j++)
	   {
           $data = $db->fetch_field($i,$j);
		   echo"<br>column $j: ";
		   print (strlen($data))? $data : "empty";
       }
   }



   // finally the usage of fetch_field
   echo "<br><br><h2>And finally the function fetch_record</h2>";
   // default arr-type MYSQL_BOTH
   $arr = $db->fetch_record(0);
   echo "<br>assoc+num array: the record shown with print_r<br>";
   print_r($arr);


   // arr-type numeric
   $arr = $db->fetch_record(0,MYSQL_NUM);
   echo "<br><br>num array: the record shown with print_r<br>";
   print_r($arr);

   // arr-type assoc
   $arr = $db->fetch_record(0,MYSQL_ASSOC);
   echo "<br><br>assoc array: the record shown with print_r<br>";
   print_r($arr);


   echo '<br><br>I hope, swemc is useful. Comments and suggestions are welcome';
?>
</body>
Return current item: swemc