?
Solved

I want to correct this piece of code

Posted on 2003-03-17
29
Medium Priority
?
304 Views
Last Modified: 2013-12-13
I found this piece of code but it doesn;t work for my case.
It doesn't coming out the fields
I want to show the fields contact_id, firstname, lastname and Birthdate
how can I do it?



<?php
// database connection stuff
$db = mysql_connect("", "", "");
mysql_select_db("myfirstdb", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");


// including the navbar class
include("./navbar.php");
// initiate it!
$nav = new navbar;
// set how many records to show at a time
$nav->numrowsperpage = 10;
$sql = "SELECT * FROM names ";


// the third parameter of execute() is optional
$result = $nav->execute($sql, $db, "mysql");
// handle the returned result set
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++) {
  $data = mysql_fetch_object($result);
  echo $data->url . "<br>\n";


}
echo "<hr>\n";
// build the returned array of navigation links
$links = $nav->getlinks("all", "on");
for ($y = 0; $y < count($links); $y++) {
  echo $links[$y] . "&nbsp;&nbsp;";
}
?>
0
Comment
Question by:vage78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 13
  • 2
29 Comments
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8158027
Where is the navclass?

Does the execute method return a results set or do you need to use the class to retrieve the data too?

0
 

Author Comment

by:vage78
ID: 8158411
Sorry RQuadling
this is navclass:

navbar.php

<?php
/*
Class navbar
Copyright Joao Prado Maia (jpm@musicalidade.net)

Sweet little class to build dynamic navigation links. Please
notice the beautiful simplicity of the code. This code is
free in any way you can imagine. If you use it on your own
script, please leave the credits as it is. Also, send me an
e-mail if you do, it makes me happy :)
 
Below goes an example of how to use this class:
===============================================
<?php
$nav = new navbar;
$nav->numrowsperpage = 3;
$sql = "SELECT * FROM links ";
$result = $nav->execute($sql, $db, "mysql");
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++) {
  $data = mysql_fetch_object($result);
  echo $data->url . "<br>\n";
}
echo "<hr>\n";
$links = $nav->getlinks("all", "on");
for ($y = 0; $y < count($links); $y++) {
  echo $links[$y] . "&nbsp;&nbsp;";
}
?>
*/

class navbar {
  // Default values for the navigation link bar
  var $numrowsperpage = 10;
  var $str_previous = "Previous page";
  var $str_next = "Next page";
  // Variables used internally
  var $file;
  var $total_records;

  // The next function runs the needed queries.
  // It needs to run the first time to get the total
  // number of rows returned, and the second one to
  // get the limited number of rows.
  //
  // $sql parameter :
  //  . the actual SQL query to be performed
  //
  // $db parameter :
  //  . the database connection link
  //
  // $type parameter :
  //  . "mysql" - uses mysql php functions
  //  . "pgsql" - uses pgsql php functions
  function execute($sql, $db, $type = "mysql") {
    global $total_records, $row, $numtoshow;

    $numtoshow = $this->numrowsperpage;
    if (!isset($row)) $row = 0;
    $start = $row * $numtoshow;
    if ($type == "mysql") {
      $result = mysql_query($sql, $db);
      $total_records = mysql_num_rows($result);
      $sql .= " LIMIT $start, $numtoshow";
      $result = mysql_query($sql, $db);
    } elseif ($type == "pgsql") {
      $result = pg_Exec($db, $sql);
      $total_records = pg_NumRows($result);
      $sql .= " LIMIT $numtoshow, $start";
      $result = pg_Exec($db, $sql);
    }
    return $result;
  }

  // This function creates a string that is going to be
  // added to the url string for the navigation links.
  // This is specially important to have dynamic links,
  // so if you want to add extra options to the queries,
  // the class is going to add it to the navigation links
  // dynamically.
  function build_geturl()
  {
    global $REQUEST_URI, $REQUEST_METHOD, $HTTP_GET_VARS, $HTTP_POST_VARS;

    list($fullfile, $voided) = explode("?", $REQUEST_URI);
    $this->file = $fullfile;
    $cgi = $REQUEST_METHOD == 'GET' ? $HTTP_GET_VARS : $HTTP_POST_VARS;
    reset ($cgi);
    while (list($key, $value) = each($cgi)) {
      if ($key != "row")
        $query_string .= "&" . $key . "=" . $value;
    }
    return $query_string;
  }

  // This function creates an array of all the links for the
  // navigation bar. This is useful since it is completely
  // independent from the layout or design of the page.
  // The function returns the array of navigation links to the
  // caller php script, so it can build the layout with the
  // navigation links content available.
  //
  // $option parameter (default to "all") :
  //  . "all"   - return every navigation link
  //  . "pages" - return only the page numbering links
  //  . "sides" - return only the 'Next' and / or 'Previous' links
  //
  // $show_blank parameter (default to "off") :
  //  . "off" - don't show the "Next" or "Previous" when it is not needed
  //  . "on"  - show the "Next" or "Previous" strings as plain text when it is not needed
  function getlinks($option = "all", $show_blank = "off") {
    global $total_records, $row, $numtoshow;

    $extra_vars = $this->build_geturl();
    $file = $this->file;
    $number_of_pages = ceil($total_records / $numtoshow);
    $subscript = 0;
    for ($current = 0; $current < $number_of_pages; $current++) {
      if ((($option == "all") || ($option == "sides")) && ($current == 0)) {
        if ($row != 0)
          $array[0] = '<A HREF="' . $file . '?row=' . ($row - 1) . $extra_vars . '">' . $this->str_previous . '</A>';
        elseif (($row == 0) && ($show_blank == "on"))
          $array[0] = $this->str_previous;
      }

      if (($option == "all") || ($option == "pages")) {
        if ($row == $current)
          $array[++$subscript] = ($current > 0 ? ($current + 1) : 1);
        else
          $array[++$subscript] = '<A HREF="' . $file . '?row=' . $current . $extra_vars . '">' . ($current + 1) . '</A>';
      }

      if ((($option == "all") || ($option == "sides")) && ($current == ($number_of_pages - 1))) {
        if ($row != ($number_of_pages - 1))
          $array[++$subscript] = '<A HREF="' . $file . '?row=' . ($row + 1) . $extra_vars . '">' . $this->str_next . '</A>';
        elseif (($row == ($number_of_pages - 1)) && ($show_blank == "on"))
          $array[++$subscript] = $this->str_next;
      }
    }
    return $array;
  }
}
?>


0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8158460
What version of PHP?

Try changing ...

 $cgi = $REQUEST_METHOD == 'GET' ? $HTTP_GET_VARS : $HTTP_POST_VARS;

to

 $cgi = $REQUEST_METHOD == 'GET' ? $_GET : $_POST;

Richard
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

Author Comment

by:vage78
ID: 8158564
php 4
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8158614
4.?

V4.2.0+ is different to V4.1.*-

Try the above mod and see if that makes any difference.

This class is written using the older (and hopefully soon defunct) register_globals settings.

Richard.
0
 

Author Comment

by:vage78
ID: 8158680
php 4_3.0.2 win 32
0
 

Author Comment

by:vage78
ID: 8158747
Hi QQuadling
I'm sorry maybe I have not explain very well.
It is working very well as navigation next previous but how can I show also the contents of my database
I have myfirstdb the name of my database and my table is names with
FirstName, LastaName, birthdate and contact_id
How can Ishow them
Can you please correct my code in order to show them
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8159025
<?php
// database connection stuff
$db = mysql_connect("", "", "");
mysql_select_db("myfirstdb", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");


// including the navbar class
include("./navbar.php");

// initiate it!
$nav = new navbar;

// set how many records to show at a time
$nav->numrowsperpage = 10;
$sql = "SELECT FirstName,LastName,BirthDate,ContactID FROM names ";

// the third parameter of execute() is optional
$result = $nav->execute($sql, $db, "mysql");

while($row = mysql_fetch_assoc($result)
     {
     echo "Name : {$row['FirstName']} {$row['LastName']}<br>Date of birth : " . date('Y-m-d',$row['BirthDate']) . "<br>Contact ID : {$row['ContactID']}<br><br>";
     }


echo "<hr>\n";
// build the returned array of navigation links
$links = $nav->getlinks("all", "on");
for ($y = 0; $y < count($links); $y++) {
 echo $links[$y] . "&nbsp;&nbsp;";
}
?>


sort of thing?
0
 

Author Comment

by:vage78
ID: 8165101
Hi RQuadling
I'm receiving this error :

Parse error: parse error, unexpected '{' in c:\program files\apache group\apache\htdocs\exam1.php on line 21
0
 

Author Comment

by:vage78
ID: 8165106
the problem seem to be :


while($row = mysql_fetch_assoc($result)
    { //here why ???
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8166389
OOps.

My typo..

while($row = mysql_fetch_assoc($result)


should be


while($row = mysql_fetch_assoc($result))

extra ) at the end.

Richard.
0
 

Author Comment

by:vage78
ID: 8172211
Hi RQuadling
the problem still exists

Parse error: parse error, unexpected '{' in c:\program files\apache group\apache\htdocs\exam1.php on line 21

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8172462
Can you put the entire script here.
0
 

Author Comment

by:vage78
ID: 8193366
Hi RQuadling
this is the code

<?php
// database connection stuff
$db = mysql_connect("", "", "");
mysql_select_db("myfirstdb", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");


// including the navbar class
include("./navbar.php");
// initiate it!
$nav = new navbar;
// set how many records to show at a time
$nav->numrowsperpage = 10;
$sql = "SELECT * FROM names ";


// the third parameter of execute() is optional
$result = $nav->execute($sql, $db, "mysql");
// handle the returned result set
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++) {
  $data = mysql_fetch_object($result);
  echo $data->url . "<br>\n";


}
echo "<hr>\n";
// build the returned array of navigation links
$links = $nav->getlinks("all", "on");
for ($y = 0; $y < count($links); $y++) {
  echo $links[$y] . "&nbsp;&nbsp;";
}
?>
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8194051
I can see nothing wrong with the code you've got.

Hmmm.

<?php
$db = mysql_connect('', '', '');
mysql_select_db('myfirstdb', $db) or die(mysql_errno() . ': ' . mysql_error() . '<br>');
include('./navbar.php');
$nav = new navbar;
$nav->numrowsperpage = 10;
$sql = 'SELECT * FROM names;';
$result = $nav->execute($sql, $db, 'mysql');
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++)
     {
     $data = mysql_fetch_object($result);
     echo $data->url . "<br>\n";
     }
echo "<hr>\n";
$links = $nav->getlinks('all', 'on');
for ($y = 0; $y < count($links); $y++)
     {
     echo $links[$y] . '&nbsp;&nbsp;';
     }
?>

(Just reformatted and converted " to ' for non embedded code).

Richard.
0
 

Author Comment

by:vage78
ID: 8208333
Hi RQuadling
I receive this error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\program files\apache group\apache\htdocs\exam2.php on line 9

--------------------------------------------------------------------------------
Previous page  1  2  3  Next page  

PS
without any DATA why??????
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8209105
What happened to the $sql?

Can you add ...

error_reporting(E_ALL);

to the top of the code and see what other errors are produced?

Also, try dropping the ; from the sql statement.

I think the class is not working properly or not returning the correct information.

The warning is synomymous with a False response from mysql_query.

Richard.
0
 

Author Comment

by:vage78
ID: 8209760
Hi RQyadling
I received this error:
Notice: Undefined property: url in c:\program files\apache group\apache\htdocs\exam2.php on line 16


Notice: Undefined property: url in c:\program files\apache group\apache\htdocs\exam2.php on line 16


--------------------------------------------------------------------------------

Notice: Undefined offset: 1 in c:\program files\apache group\apache\htdocs\navbar.php on line 85

Notice: Undefined variable: query_string in c:\program files\apache group\apache\htdocs\navbar.php on line 96
Previous page  1  2  3  Next page  
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8209822
Looks like the navbar class is not right.

What version of PHP is the server running?
0
 

Author Comment

by:vage78
ID: 8215812
php.4.3.0 win 32
But the strange thing is that without requesting data
the navbar is working. It gives me the
menu previous pages 1 2 3 etc
when I'm trying to take output from data then comes the problem
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8216810
Do you have this online?
0
 

Expert Comment

by:baeuml
ID: 8217859
Hi!

Your script is not working, because in $db you only have a connection link to mysql, but not to a specific database.

Your code should be

$connection = mysql_connect('', '', '');
$db = mysql_select_db('myfirstdb', $connection);

Then it should work...

-baeuml
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8217903
Quite often, users are assigned to a default database, so the mysql_select_db can often be redundant.

Richard.
0
 

Expert Comment

by:baeuml
ID: 8218196
Yeah right, but the navbar class expects the $db to be a database link and not a connection link...

If it's a connection link, the navbar class doesn't query the database and therefor mysql_num_rows produces an error because $result is empty.

-baeuml
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8218229
Yep!
0
 

Author Comment

by:vage78
ID: 8222979
Hi baeuml
the error now is "
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in c:\program files\apache group\apache\htdocs\exam2.php on line 4

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in c:\program files\apache group\apache\htdocs\exam2.php on line 7 "

and my code is
<?php
//exam2.php
//$db = mysql_connect('', '', '');
$db = mysql_select_db('myfirstdb', $connection);
error_reporting(E_ALL);

mysql_select_db('myfirstdb', $db) or die(mysql_errno() . ': ' . mysql_error() . '<br>');
include('./navbar.php');
$nav = new navbar;
$nav->numrowsperpage = 10;
$sql = 'SELECT * FROM names';
$result = $nav->execute($sql, $db, 'mysql');
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++)
    {
    $data = mysql_fetch_object($result);
    echo $data->url . "<br>\n";
    }
echo "<hr>\n";
$links = $nav->getlinks('all', 'on');
for ($y = 0; $y < count($links); $y++)
    {
    echo $links[$y] . '&nbsp;&nbsp;';
    }
?>


0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8223647
No!!!

mysql_select_db returns a boolean and simply acts upon the connection.

$db in the class IS the db connection!

Look at line in the class ..

 $result = mysql_query($sql, $db);


and then the PHP definition of mysql_query ...

resource mysql_query ( string query [, resource link_identifier [, int result_mode]])

Link Idenitifier, not database name (string) and definitely NOT boolean!!!


Try ...


<?php
//exam2.php

// Set error logging BEFORE any action takes place!
error_reporting(E_ALL);

// Include any required classes.
include('./navbar.php');

// Make a connection to the DB server.
$db = mysql_connect('put_db_server_name_here', 'put_your_db_user_name_here', 'put_your_db_password_here');

// Select your db (optional, but may be necessary if your user is not given a specific db already).
mysql_select_db(put_your_db_name_here', $db);

$nav = new navbar;
$nav->numrowsperpage = 10;
$sql = 'SELECT * FROM names';
$result = $nav->execute($sql, $db, 'mysql');
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++)
   {
   $data = mysql_fetch_object($result);
   echo $data->url . "<br>\n";
   }
echo "<hr>\n";
$links = $nav->getlinks('all', 'on');
for ($y = 0; $y < count($links); $y++)
   {
   echo $links[$y] . '&nbsp;&nbsp;';
   }
?>


You also took out the connection to the Db!!!

So...

0
 

Author Comment

by:vage78
ID: 8223850
No I receive this error
"Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in c:\program files\apache group\apache\htdocs\exam2.php on line 18"
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 90 total points
ID: 8224087
mysql_select_db('put_your_db_name_here', $db);

Missed opening quote.

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question