Previous Next Table of Contents

11. Examples

To build each of the non-SQL examples:

  cgi++ -o example.cc example.cgi++
  g++ -o example.cgi example.cc -lcgi++
  

Depending on your installation you may need to specify the proper paths to cgi++-0.8.tar.gzlib.h and libcgi++.a with -I and -L when invoking g++. If the example is using SQL macros you will need to add -lmysqlclient and appropriate -I and -L to find the includes and the libraries.

11.1 Hello, world

   MAIN_START
    HTML_START
     <html> <body>
      Hello, world
      </body></html>
    HTML_END
   MAIN_END
  

11.2 Hello, form

 MAIN_START
 HTML_START
  <form name=form1 method=post>
   Socks <input type=radio name=r value=Socks><br> 
   Shoes <input type=radio name=r value=Shoes><br> 
  <input type=submit name=submit value=Submit>
  </form>
 HTML_END
 if(form1)
  {
   HTML_START
   So you like $form1.r$ hmm...
   HTML_END
  }

MAIN_END
 
  

11.3 SQL example

This example assumes you have mysqld running on localhost, you have user scott with the password tiger that has insert and select priviliges in the database test. You must have table stuff(i int, s char(20)) in the test database. You must also have mysql client library and include files installed to run this example.

   SQL_DB_DRIVER(mysql)
   SQL_ON_ERROR(sql_error)
   SQL_DECL_DBH(dbh)

   void sql_error()
    {
     HTML_START
      Database error: $mysql_error(&dbh)$
     HTML_END
     exit(1);
    }

   MAIN_START
    int i;
    char* s = "Hello, SQL";

    SQL_CONNECT(localhost, test, scott, tiger)
    for(i = 1; i < 10; i++)
     {
      SQL_DO("insert into stuff values ( $i$, '$s$')")
     }
    HTML_START
     <table>
      <tr><td>i</td><td>s</td></tr>
    HTML_END

    SQL_CURSOR_DO("select * from stuff")
     HTML_START
      <tr><td> $sql_row[0]$ </td><td>$sql_row[1]$ </td></tr>
     HTML_END
    SQL_CURSOR_DONE
  
    HTML_START
     </table>
    HTML_END

   MAIN_END

  

11.4 Nested SQL_CURSOR loop



SQL_DB_DRIVER(mysql)
SQL_ON_ERROR(sql_error)
SQL_DECL_DBH(dbh)

 void sql_error()
   {
    HTML_START
     Database error: $mysql_error(&dbh)$
    HTML_END
    exit(1);
   }


 MAIN_START
   int i;
   char* s = "sasha's test";
   string s1 = "hello, world";

   SQL_CONNECT(localhost, test, scott, tiger)
   HTML_START
    <ul>
   HTML_END

   SQL_CURSOR_DO("select distinct i from stuff1")
    HTML_START
      <li> $sql_row[0]$
      <ul>
    HTML_END
    
    SQL_CURSOR_DO("select s from stuff where i = $sql_row[0]$")
     HTML_START
      <li> $sql_row1[0]$
     HTML_END
    SQL_CURSOR_DONE
    
    HTML_START
      </ul>
     HTML_END

   SQL_CURSOR_DONE
 
   HTML_START
    </ul>
   HTML_END

 MAIN_END

11.5 Cookie Example



HTTP_COOKIE_CLASS(my_cookie, 3600, "", "", id, name)
/*
  Create an HTTP cookie class which will process cookies with the name of 
   my_cookie that will expire 3600 secs after they are issued with default domain
   and path and cgi++-0.8.tar.gzencoded values with the name of id and name.
 */

HTTP_COOKIE_MAKER(make_cookie)
/* function make_cookie is responsible for printing the value of the cookie to
 stdout */

void make_cookie()
 {
   my_cookie.id = "3";
   my_cookie.name = "bobo";
   cout << my_cookie;
 }

MAIN_START
 HTML_START
  Did my cookie taste good?
 HTML_END

 if(my_cookie)
  {
 HTML_START
  Your browser sent me a cookie: id = $my_cookie.id$ name = $my_cookie.name$
 HTML_END
  }
MAIN_END
 

11.6 Schema Generation Example

 
SQL_CLIENT_APP("mysql -uscott -ptiger -f test")
SQL_SCHEMA_FILE("test_mk_db.sh")
SQL_FORM_TO_TABLE("form1")
SQL_FORM_TO_TABLE(form2)

MAIN_START
 HTML_START
  <form name=form1 method=get>
   Shirts <input type="radio" name="r" value="Shirts" storage="char(20)" ><br> 
   Socks <input type=radio name="r" value=Socks><br> 
   Shoes <input type="radio" name=r value=Shoes><br> 
  <input name=words storage="char(50)">
  <input type=submit name=submit1 value=Submit>
  </form>
  <p>

  <form name=form2 method=post>
   Cats <input type="radio" name="r" value="Cats"><br> 
   Dogs <input type=radio name="r" value=Dogs><br> 
   Some text: <input name=some_text><br>
   Textarea: <textarea name=much_text></textarea><br>
   Checkbox: <input type=checkbox name=check_me><br>
   List: 
    <select name=list>
     <option value=Linux>Linux
     <option value=BSD>BSD
     <option value="Sun OS">SunOS
    </select> 
  <input type=submit name=submit2 value=Submit>
  </form>
 HTML_END
 if(form1.submit1.length())
  {
   HTML_START
   So you like $form1.r$ hmm...
   HTML_END
  }

if(form2.submit2.length())
 {
   HTML_START
   So you like $form2.r$ hmm...
   HTML_END

 }

MAIN_END

 

11.7 Mail Example


MAIL_ON_ERROR("mail_error")
MAIL_APP("/usr/sbin/sendmail -t")

void mail_error()
 {
  HTML_START
   Could not send mail.
  HTML_END
  exit(0);
 }

MAIN_START

 if(!form1)
  {
   HTML_START
    <form name=form1>
     Click the button to send a message :<br>
     <input type=submit name=submit value=Submit>
    </form>
   HTML_END
  }
 else
  {
   string from = "Bill Clinton <president@whitehouse.org>";
   string to = "you@yourcompany.com";
   string reply_to = "yourboss@yourcompany.com";
   string subject = "Microsoft";

   MAIL_INIT($from$, $to$, $reply_to$, $subject$)
   MAIL_START
    Dear Open Source Advocate:

    Tomorrow Microsoft will be required to make the source code of Windows
    NT publically available for download from their FTP site with detailed
    documentation.

    Bill Clinton.

   MAIL_END
   MAIL_CLOSE
   HTML_START
    The message  has been sent
   HTML_END
  }

MAIN_END


 


Previous Next Table of Contents