#native_company# #native_desc#
#native_cta#

Building WML Sites Page 6

By Andres Baravalle
on February 21, 2001

Writing the query

This file is the one responsible for handling the query. It’s name is query.wml
and we will analyze it more in depth.

<?php

Header("Content-type: text/vnd.wap.wml");

printf("<?xml version="1.0"?>n");

printf("<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "

        
.""http://www.wapforum.org/DTD/wml_1.1.xml" >n");

printf("<wml>n");

//    The next lines are used to build

//    the sql query for receiving hours:

$consulting_tables =

    
"(professors left join teach on (professors.Id = teach.Id), subjects)";

$consulting_columns =

    
"professors.Surname, professors.Name, subjects.Subject , ";

$consulting_columns .=

    
"subjects.Cod_number, professors.Consulting_hour, professors.Consulting_place";

$consulting_query=

    
"subjects.Cod_Subject = teach.Cod_subject ";

//    The next lines are used to build the

//    sql query for examination timetables:

$exams_tables"(exams left join professors ON (exams.Id = professors.Id), subjects)";

$exams_columns"subjects.Subject , subjects.Cod_number, professors.Surname, ";

$exams_columns.= "professors.Name, exams.Date, exams.Time, exams.Room, exams.Test";

$exams_query"exams.Cod_Subject = subjects.Cod_Subject ";

//  The next lines are used to add options to the sql query for examination timetables:

if ($exams_data) {

    switch($exams_data) {

        case 
"subject":

            
$exams_query.= " and subjects.Subject like '%$subject%'";

            break;

        case 
"surname":

            
$exams_query.= " and professors.Surname like '%$surname%'";

            break;

    }

}

// The next lines are used to to add options to the sql query for receiving times:

if ($consulting_data) {

    switch(
$consulting_data) {

        case 
"subject":

            
$consulting_query

                
.= " and subjects.Subject like '%$subject%'";

            break;

        case 
"surname":

            
$consulting_query.= " and professors.Surname like '%$surname%'";

            break;

    }

}

// handles the connection with database

function connect($tables$data$condition_passed) {

    
//

    //    put your password and username in next line

    //

    $db mysql_pconnect("localhost","***","***");

    // put your database name in next line

    mysql_select_db("lanfranchi_co_uk",$db);

    $sql "SELECT $data FROM $tables WHERE $condition_passed order by professors.Surname";

    
$result mysql_query($sql,$db);

    return 
$result;

}

// this function writes the wml code for receiving hours

function consulting_print($consulting_result) {

    global 
$file;

    
printf("<card id="card1" title="hours">n");

    
printf("<p>Receiving hours</p>n");

    

    while (
$myrow mysql_fetch_row($consulting_result)) {

        
printf("<p>$myrow[0], $myrow[1]</p>n");

        
printf("<p>$myrow[2]</p>n");

        
printf("<p>$myrow[3]</p>n");

        
printf("<p>$myrow[4]</p>n");

        
printf("<p>$myrow[5]</p>n");

    }

    
printf("</card>n");

}

// this function writes the wml code for examination timetables

function print_exams($exams_result) {

    global 
$file;

    
printf("<card id="card1" title="hours">n");

    
printf("<p>Examinations hours</p>n");

    while (
$myrow mysql_fetch_row($exams_result)) {

        
printf("<p>$myrow[2], $myrow[3]</p>n");

        
printf("<p>$myrow[0]</p>n");

        
printf("<p>$myrow[1]]</p>n");

        
printf("<p>$myrow[4], $myrow[5]</p>n");

        
printf("<p>$myrow[7]</p>n");

        
printf("<p>$myrow[6]</p>n");

    }

    
printf("</card>n");

}

// checks if you selected reciving hours or examination

//timetables, connects to the database and calls the

//function to the write wml code

if ($consulting_data) {

    
$connection_result =

        
connect($consulting_tables$consulting_columns$consulting_query);

    
consulting_print($connection_result);

}

if (
$exams_data) {

    
$connection_result =

        
connect($exams_tables, $ exams_columns, $ exams_query);

    
print_exams($connection_result);

}

printf("</wml>n");

?>



The End

We have finished. Your first PHP/WML pages are ready to get the data
from your MySQL database.
— Andres

1
|
2
|
3
|
4
|
5
|
6