Categories > TinyButStrong general >

Problem with Navbar and Variables...

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: B-Rad
Date: 2008-09-08
Time: 10:08

Problem with Navbar and Variables...

Greetings... This is my first time working with TBS, and thus far I am highly pleased, since I am also not a programmer.

So far, I have been able to setup a database, add tables, fields and data (Nothing complicated). Based on some reading, and examples, I have managed to put together the PHP script, and have also designed a template, to display the search results from the DB. (hard to believe I did this much myself so far)

Really, the only issue I have is that I cannot get the 'By Page' plugin working correctly. It works as far as returning the initial search, and showing the pagination. However, none of the pagination links work correctly. I managed to modify the link code so that the script link actually went to the correct place, but it seems that I am loosing the variables once the page (template) loads???

I have searched this forum, but have not been able to find anything related. I assume that this is something silly that I am missing, but here is my script code:

        <?php
        include_once('../includes/tbs_class_php5.php');
        include_once('../includes/tbs_plugins/tbs_plugin_bypage.php'); // Plug-in By-Page
        include_once('../includes/tbs_plugins/tbs_plugin_navbar.php'); // Plug-in Navigation Bar

        //Connexion to the database
        //if (!isset($_SERVER)) $_SERVER=&$HTTP_SERVER_VARS ; //PHP<4.1.0
        //require($_SERVER['DOCUMENT_ROOT'].'./cnx_mysql.php');
        require('../includes/cnx_mysql.php');

            // Default value
            if (!isset($_GET)) $_GET=&$HTTP_GET_VARS ;
            if (isset($_GET['PageNum'])) {
              $PageNum = $_GET['PageNum'];
            } else {
                $PageNum = 1;
            }

            // Default value
            if (isset($_GET['RecCnt'])) {
              $RecCnt = intval($_GET['RecCnt']);
            } else {
                $RecCnt = -1;
            }

            $PageSize = 7;           

        $TBS =& new clsTinyButStrong ;
        $TBS->LoadTemplate('../includes/search_include.php') ;
        //$TBS->MergeBlock('blk1',$cnx_id,'SELECT * FROM pdf_quicksearch LIMIT 0,5') ;

            // Merge the block by page
            $TBS->PlugIn(TBS_BYPAGE,$PageSize,$PageNum,$RecCnt); // Next block will be merged suing By-Page mode.
            // $RecCnt = $TBS->MergeBlock('blk',$data);
            $mysql = "SELECT * FROM pdf_quicksearch WHERE ID LIKE '%$part%'" ;
            $RecCnt = $TBS->MergeBlock('blk1', $cnx_id, $mysql);

            // Merge the Navigation Bar
            $TBS->PlugIn(TBS_NAVBAR,'nv','',$PageNum,$RecCnt,$PageSize);

        mysql_close($cnx_id) ;
        $TBS->Show() ;
        ?>



And the template code for the Navbar:


         <table border="1" align="center" cellpadding="2" cellspacing="0" width="700" class="searchByPage">
          <tr align="center">
          <td width="16"
              onmouseover="javascript:style.backgroundColor='#FFCC99'; javascript:style.color='#ffffff'; javascript:style.cursor='pointer';"
              onclick="javascript:location.href = './[var..script_name]?PageNum=[nv.first;endpoint;magnet=a;mtype=m+m]';"
              onmouseout="javascript:style.backgroundColor='#430F11'; javascript:style.color='#FFFFFF'; 
          "><a href="./[var..script_name]?PageNum=[nv.first;endpoint;magnet=a;mtype=m+m]">|&lt;</a></td>

          <td width="16"
              onmouseover="javascript:style.backgroundColor='#FFCC99'; javascript:style.color='#ffffff'; javascript:style.cursor='pointer';"
              onclick="javascript:location.href = './[var..script_name]?PageNum=[nv.prev;endpoint;magnet=a;mtype=m+m]';"
              onmouseout="javascript:style.backgroundColor='#430F11'; javascript:style.color='#FFFFFF'; 
          "><a href="./[var..script_name]?PageNum=[nv.prev;endpoint;magnet=a;mtype=m+m]">&lt;</a></td>

          <td width="16"
              onmouseover="javascript:style.backgroundColor='#FFCC99'; javascript:style.color='#ffffff'; javascript:style.cursor='pointer';"
              onclick="javascript:location.href = './[var..script_name]?PageNum=[nv.page;block=td;navsize=5;navpos=centred]';"
              onmouseout="javascript:style.backgroundColor='#430F11'; javascript:style.color='#FFFFFF'; 
          "><a href="./[var..script_name]?PageNum=[nv.page;block=td;navsize=5;navpos=centred]">[nv.page]</a></td>

          <td width="16" bgcolor="#FFCC99"><strong>[nv.page;block=td;currpage]</strong></td>

          <td width="16"
              onmouseover="javascript:style.backgroundColor='#FFCC99'; javascript:style.color='#ffffff'; javascript:style.cursor='pointer';"
              onclick="javascript:location.href = './[var..script_name]?PageNum=[nv.next;endpoint;magnet=a;mtype=m+m]';"
              onmouseout="javascript:style.backgroundColor='#430F11'; javascript:style.color='#FFFFFF'; 
          "><a href="./[var..script_name]?PageNum=[nv.next;endpoint;magnet=a;mtype=m+m]">&gt;</a></td>

          <td width="16"
              onmouseover="javascript:style.backgroundColor='#FFCC99'; javascript:style.color='#ffffff'; javascript:style.cursor='pointer';"
              onclick="javascript:location.href = './[var..script_name]?PageNum=[nv.last;endpoint;magnet=a;mtype=m+m]';"
              onmouseout="javascript:style.backgroundColor='#430F11'; javascript:style.color='#FFFFFF'; 
          "><a href="./[var..script_name]?PageNum=[nv.last;endpoint;magnet=a;mtype=m+m]">&gt;|</a></td>

          </tr>[onshow;block=table;when [var.RecCnt;noerr]+-7]
          </table>



And here is the resulting code from the source of the browser with a 12 record result, displaying 7 records per page:

        <table border="1" align="center" cellpadding="2" cellspacing="0" width="700" class="searchByPage">
          <tr align="center">
          <td width="16"
              onmouseover="javascript:style.backgroundColor='#FFCC99'; javascript:style.color='#ffffff'; javascript:style.cursor='pointer';"
              onclick="javascript:location.href = './search_results.php?PageNum=';"
              onmouseout="javascript:style.backgroundColor='#430F11'; javascript:style.color='#FFFFFF'; 
          ">|&lt;</td>

          <td width="16"
              onmouseover="javascript:style.backgroundColor='#FFCC99'; javascript:style.color='#ffffff'; javascript:style.cursor='pointer';"
              onclick="javascript:location.href = './search_results.php?PageNum=';"
              onmouseout="javascript:style.backgroundColor='#430F11'; javascript:style.color='#FFFFFF'; 
          ">&lt;</td>


          <td width="16" bgcolor="#FFCC99"><strong>1</strong></td><td width="16"
              onmouseover="javascript:style.backgroundColor='#FFCC99'; javascript:style.color='#ffffff'; javascript:style.cursor='pointer';"
              onclick="javascript:location.href = './search_results.php?PageNum=2';"
              onmouseout="javascript:style.backgroundColor='#430F11'; javascript:style.color='#FFFFFF'; 
          "><a href="./search_results.php?PageNum=2">2</a></td>

          <td width="16"
              onmouseover="javascript:style.backgroundColor='#FFCC99'; javascript:style.color='#ffffff'; javascript:style.cursor='pointer';"
              onclick="javascript:location.href = './search_results.php?PageNum=2';"
              onmouseout="javascript:style.backgroundColor='#430F11'; javascript:style.color='#FFFFFF'; 
          "><a href="./search_results.php?PageNum=2">&gt;</a></td>

          <td width="16"
              onmouseover="javascript:style.backgroundColor='#FFCC99'; javascript:style.color='#ffffff'; javascript:style.cursor='pointer';"
              onclick="javascript:location.href = './search_results.php?PageNum=2';"
              onmouseout="javascript:style.backgroundColor='#430F11'; javascript:style.color='#FFFFFF'; 
          "><a href="./search_results.php?PageNum=2">&gt;|</a></td>

          </tr>
          </table>

Now, if I clicked the link for the second  page, instead of returning the rest of the previous result (5 records), It now returns the result of the whole table (220 records).

The reason I believe I am loosing variables, is based on the link
<a href="[var..script_name]?PageNum=
which I assume would return the full path or URL to the script, including the name, but it does not. It only inserted the script name itself, which is why I added the './' in front of it. Other wise it was a broken link.

Thanks in advance for any help, or direction.
By: TomH
Date: 2008-09-08
Time: 15:44

Re: Problem with Navbar and Variables...

Hi

(1) Sorry, I do not understand your code as shown... the var "$part" (in the mysql query) is not defined... so how is the query able to be processed correctly?

(2) My personal opinion... the BYPAGE approach is not good practice to be used.  May be good to learn better practice and use that consistently.

It is easy (with MySQL, MS-SQL, maybe others) to use NAVBAR and no BYPAGE. Just querying only the needed records directly by coding the LIMIT clause in a simple way.

You can see a complete example at http://tomhenry.us/tbs3/  Go to the "Efficient Paging" example (at the bottom left of the page).

Hope that helps a little,
TomH
By: B-Rad
Date: 2008-09-08
Time: 23:33

Re: Problem with Navbar and Variables...

TomH; Thanks for the heads up. I have already visited your Site (I found it during my previous reading). If I remember correctly, you are using the ezSQL, and that kind of threw me.

I was able to get the ezSQL demo working, but everything else just returned a blank screen. I would love to get some info on using the two together, but I will revisit, and try to figure it out.

In the mean time, it is totaly possible that I have not coded something correctly, in which case I would like to fix it. The var "$part" it is received from a form variable (part) on submission of the search to the PHP side.

Does that help to better understand?    Thanks again!
By: TomH
Date: 2008-09-09
Time: 02:04

Re: Problem with Navbar and Variables...


The query statements are the same whether using the ezSQL class or not.

Use a stripped down version of your scripts (especially the template) to be sure the queries are returning the expected results before you get fancy with the output.

Using just mysql, your script might might need something like the following code...
$link = mysql_connect("localhost", "username", "password") or
die("Could not connect: " . mysql_error());
mysql_select_db("dbname");

$mysql = "SELECT SQL_CALC_FOUND_ROWS * FROM pdf_quicksearch WHERE ID LIKE '%$part%'" ;

$result = mysql_query($mysql,$link);

$object = mysql_fetch_object($result);

// $out = print_r($object, true); // uncomment to debug
// echo "OUT: $out<hr>";        // uncomment to debug

$rowresult = mysql_query("SELECT FOUND_ROWS()", $link);

$rows=mysql_fetch_array($rowresult);
$allrows=$rows["FOUND_ROWS()"];

// echo "ROWS: $allrows<hr />";  // uncomment to debug

I tested this with one of my db's so the code should work with only a little fudging

By: B-Rad
Date: 2008-09-09
Time: 03:42

Re: Problem with Navbar and Variables...

Thanks for the info in the last post! I was able to use it with the debug info, and verify that the connection to the database is working, and I am getting the results I want. Basically, it returned the same results that I am getting into my template.

I was able to get my PHP script working with the ezSQL, and code borrowed from your site after going back over and reading the info again.

The revised code now reads as follows:

        <?php
        include_once "../includes/ezsql/shared/ez_sql_core.php";
        include_once "../includes/ezsql/mysql/ez_sql_mysql.php";
        include_once('../includes/tbs_class_php5.php');
        include_once('../includes/tbs_plugins/tbs_plugin_navbar.php'); // Plug-in Navigation Bar
        include_once('../includes/tbs_plugins/tbsdb_ezsql.php'); // Plug-in ezSQL

        $db = new ezSQL_mysql($db_user, $db_password, $db_name, $db_host);

        $TBS =& new clsTinyButStrong ;
        $TBS->LoadTemplate('../includes/search_include.php') ;

                    // check if $PageNum param passed and set default
                    $PageNum = isset($_GET['PageNum'])?(int)$_GET['PageNum']:1;
                    $PageSize = 10;       // set the desired rows per page
                    // set the LIMIT offset for mySQL query based on which page we're on now
                    $StartIndex = $PageSize*($PageNum-1);

                    // the MySQL query stmt for the variable LIMIT clause:
                    $mysql = "SELECT SQL_CALC_FOUND_ROWS * FROM pdf_quicksearch WHERE ID LIKE '%$part%' LIMIT $StartIndex,$PageSize" ;
                    $query = $db->get_results($mysql);       // do the ezSQL query using SQL_CALC_FOUND_ROWS trick

                    // from the db query result set the remaing vars for the NAVBAR plugin
                    $found = $db->get_row("SELECT FOUND_ROWS() as rows");

                    $RecCnt = $found->rows;

                    $TBS->MergeBlock('blk1', $query);

            // Merge the Navigation Bar
            $TBS->PlugIn(TBS_NAVBAR,'nv','',$PageNum,$RecCnt,$PageSize);

        $TBS->Show() ;
        ?>

Even with these changes, I am still getting the same result. After doing a search that returned 16 records, the initial 10 were displayed, and two page links were shown in the navbar. After clicking on the link for page 2, I am given the result of the whole table (220 records).

However, I was able to figure out one other thing, and that is that I was correct in assuming that I was loosing a variable. The page 2 link looked like this:

     results.php?PageNum=2

by changing it in the browser to this:

     results.php?PageNum=2&part=fl
     (the part originally searched for)

I am now presented with the final results of the query. So, it seems that somewhere along the line, I am loosing the var '$part'.

What would be the best way of passing this var along to the other "pages"?
By: B-Rad
Date: 2008-09-09
Time: 05:10

Re: Problem with Navbar and Variables...

I also have an error now that I didn't have previously.

Before I changed the code to use the ezSQL, if I did a search for something that didn't exist, I jsut got "0 Record(s) Found", and nothing else.

Now, if I do a search for something that doesn't exist, I get an error stating "TinyButStrong Error when merging block [blk1] : unsupported variable type : 'NULL'.". I still the "0 Record(s) Found", but it also shows all the [blk1...] place holders, within the HTML tables.

Other then that, the new code works just fine. How would I figure out what is causing this error?
By: B-Rad
Date: 2008-09-09
Time: 05:13

Re: Problem with Navbar and Variables...

just to clarify, other than not passing the var '$part' to the other "pages", and the new error when no results are found, it is working just fine.
(:
By: TomH
Date: 2008-09-09
Time: 13:20

Re: Problem with Navbar and Variables...

Glad you're making progress!

You mentioned that your are initiating the search from a form that passes the value of $part.  So you need to pass that var/val forward when you allow the user to select from the NAVBAR -- but you really figured that out already... now just add that bit of code to the nav bar bby adding the variable part=[var.part] , something like
<td width="16"
     onmouseover="javascript:style.backgroundColor='#FFCC99'; javascript:style.color='#ffffff'; javascript:style.cursor='pointer';"
     onclick="javascript:location.href = './[var..script_name]?part=[var.part]&PageNum=[nv.page;block=td;navsize=5;navpos=centred]';"
     onmouseout="javascript:style.backgroundColor='#430F11'; javascript:style.color='#FFFFFF';
    "><a href="./[var..script_name]?part=[var.part]&PageNum=[nv.page;block=td;navsize=5;navpos=centred]">[nv.page]</a></td>

As far as the error when there are no results... this is partly an issue of the design: either (a) only showing the user choices (in the form) that prodice actual results -or- (b) you have to handle the no data sition as an error at the point where the PHP code gets no rows back from the query.

Personally, I would prefer to only show the user choices that are valid when that choose from the form. Look at the mysql "SELECT DISTINCT " query to get a list from the database of only those "part"s that actually exist - and build your form selection list from that.
By: B-Rad
Date: 2008-09-10
Time: 01:01

Re: Problem with Navbar and Variables...

Thanks!!!

You know, I did try that, but I couldn't get it to work - I was placing that code after the 'PageNum' data. Using your example, and placing it before the 'PageNum' data, it worked fine. ???

Now all that's left is that error message.

I went back and tested again with my old code and it works fine (although it is using the 'ByPage' code), but when I switch back to the new code using ezSQL, I get the error message (searching for something I know it won't find).

I have managed to hide the template tables, and only show the '0 Record(s) Found' message by using

   
[onshow;block=table;when [var.RecCnt]+-0]

and even though I have placed

    
$TBS->NoErr = true;

in the script, and in the block merge, it still shows the error:

     TinyButStrong Errorwhen merging block [blk1;noerr] : unsupported variable type : 'NULL'.

Would it be possible to modify the ezSQL code to return something other than 'NULL'?

How else could I handle this error?

Thanks Again!!!
By: TomH
Date: 2008-09-10
Time: 01:49

Re: Problem with Navbar and Variables...

As mentioned in prev post you need to decide which way to handle the 'no query result' condition (no value for $part is found)

As before I prefer the (a) option.

If you instead choose the (b) option you mighht try something like this modification to your PHP...
if( $query = $db->get_results($mysql) ){
    $found = $db->get_row("SELECT FOUND_ROWS() as rows");
    $RecCnt = $found->rows;
    $TBS->MergeBlock('blk1', $query);
    $TBS->PlugIn(TBS_NAVBAR,'nv','',$PageNum,$RecCnt,$PageSize);
}else{
$norows_message ="No data found for $part";
}

$TBS->Show();

Add [var.norows_message] somewhere in your HTML with some conditional display logic.

By: B-Rad
Date: 2008-09-10
Time: 06:25

Re: Problem with Navbar and Variables...

Thanks again TomH, you pointed me in the right direction.

I will already have a drop down list where they can choose a specific item which is why I didn't want to do another. Also, that would defeat the whole search option where they can enter a partial name and get all similar results.

I tried the code you provided, but I got even more errors in addition to the one I was trying to get rid of, but as I mentioned, it did get me pointed in the right direction, and I was able to get it all straightened out.

The final piece of code that fixed it was:

    
                    $NewQuery = $db->get_results($mysql);       // do the ezSQL query using SQL_CALC_FOUND_ROWS trick
                   
                    // from the db query result set the remaing vars for the NAVBAR plugin
                    $found = $db->get_row("SELECT FOUND_ROWS() as rows");
                    $RecCnt = $found->rows;

                    // check for NULL error - non existant reslut
                    IF ( $RecCnt == 0 ) { $TBS->MergeBlock('blk2', clear);
                    } else {
                        $TBS->MergeBlock('blk1', $NewQuery);
                    }
            // Merge the Navigation Bar
            $TBS->PlugIn(TBS_NAVBAR,'nv','',$PageNum,$RecCnt,$PageSize);
        $TBS->Show() ;

Once again, thanks for the assistance. It was greatly appreciated.

[ I think I may need a programming class or two. ] (-: