Jump to content

Bazzaah

Members
  • Posts

    49
  • Joined

  • Last visited

Posts posted by Bazzaah

  1. Indeed - I'd seen you give this answer in another thread. I thought it was there to separate the results into rows so that the $list['word'] would work.

     

    I'd been deleting some superfluous code and notes from the script and should have kept trimming!

     

    Thanks, though - all sorted!

  2. Hi

     

    I have this code to echo th econtents of a table, which contains 100 rows.

     

    I want to be able to display all 100 rows.

     

    The problem is that it displays 99 (it excludes the first).

     

    I've tried to backtrack to reconstruct the code to find out where the error is but no joy.

     

    Any ideas?

     

    Thanks in advance!

     

    
    ....
    
    // get all entries from table
    
    $sql = "SELECT * FROM 100words ORDER BY word_id";
    $result = mysqli_query($dbc, $sql);
    $r = mysqli_fetch_row($result);
    
    //print table entries to screen in columns
    
    echo '<div id="container">';
    
    // results presented in html table 5 columns, 20 rows per page
    	echo '<div id="outerbox">';
                    echo '<div class="innerbox">';
    	echo '<table class="centerresults" border="0">'  . "\n";
    	echo '<td>';  
      
    	$i = 0;    
    	$max_columns = 5;    
    
        while ($list = mysqli_fetch_assoc($result)) {  
    
          extract($list);	
    
    		// open row if counter is zero       
    
    		if($i == 0) 
    		         
    		echo '<tr>' . "\n"; 
    				         
    		echo '<td width="150px"><a href="word.php?w=' .
    
    		$list['word'] . '">' . 
    
    		$list['word'] . "</a></td> \n ";  
    		         
    
    		// increment counter - if counter = max columns, reset counter and close row       
    
    		if(++$i == $max_columns) { 
    			          
    			echo '</td>' . "\n"; 
    			          
    			$i=0;  
    			     
    		}  // END if(++$i == $max_columns) { 
    
    	} // END while (!empty($myArray)) {
    
    //} END while ($list = mysql_fetch_array($result)) { 
    
    
    //END if($i < $max_columns) {     
    
    	echo '</tr>' . "\n"; 
    
    	echo '</table>' . "\n"; 
    
       echo '</div>';
       
          echo '</div>';
    
    

  3. ScotDiddle

     

    Thanks so much for that, it's really appreciated.

     

    One further question if I may about using an array; the table is built from a search which I've set up like this;

     

    $sql = "SELECT * FROM words WHERE word LIKE '%{$searchTermDB}%' ORDER BY word LIMIT $offset, $rowsperpage";
    $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
    

     

    (I should update to mysqli functions.)

     

    If the results are expressed through the variable $result, how do I use an array?

     

  4. Hi there

     

    I followed this post

     

    http://www.phpfreaks.com/forums/index.php?topic=95426.0

     

    to get a multi-column layout of search results.

     

    All's well but I seem to have an extra blank row at the bottom of the table.

     

    This is my code for the table

     

    echo '<table width="800px" class="center" border="1">';
    echo '<td>';  
    
    if($result && mysql_num_rows($result) > 0)
    {
        $i = 0;
        $max_columns = 3;
        while ($list = mysql_fetch_array($result))       
       {
           // make the variables easy to deal with
           extract($list);
    
           // open row if counter is zero
           if($i == 0)
              echo "<tr>";
              echo '<td><a href="word.php?w=' . $list['word'] . '">' . $list['word'] . '</a></td>';
        
           // increment counter - if counter = max columns, reset counter and close row
           if(++$i == $max_columns) 
           {
               echo "</tr>";
               $i=0;
           }  // end if 
       } // end while
    } // end if results
    
    // clean up table - makes your code valid!
    if($i < $max_columns)
    {
        for($j=$i; $j<$max_columns;$j++)
            echo "<td> </td>";
    }
    echo '</tr>';
    echo '</table>';
    

     

    Any ideas on how not to have it there?

     

    Thanks in advance.

     

  5. I'm a bit confused with some CSS I've been doing and wondered if someone could help me out please.

     

    I have my header and footer in place;

     

    html,
    body {
       background: #2c539e;
       font-size: 100%;
       margin:0;
       padding:0;
       height:100%;
    }
    
    /* container */
    
    #centered {
       min-height:100%;
       position:relative;
       margin: 20px auto;
    
    }
    /* header and footer*/
    
    #header {
       background:#ff0;
       margin-top: 10px;
    }
    
    #footer {
       position:absolute;
       bottom:0;
       width:100%;
       height: 60px;  
       margin-bottom: 10px;
    }
    

     

     

    The content is contained in a couple of (nested?) boxes.

     

    #frontpagecentered{
      margin-top: 240px;
      position: absolute;
      width: 950px;
      left: 50%;
      margin-left: -475px; 
      margin-right:auto;
      background-color: #9abdde;
      padding-top: 30px;
      padding-bottom: 30px;
      	-moz-border-radius: 15px;
    border-radius: 15px;
    
    }
    
    #box {
    
       padding:10px 10px 10px 10px;
       padding-bottom:60px;   /* Height of the footer */
       width: 900px;
       background-color: #F0F2F7;
       font-size: 1.1em;
       font-family: Century Gothic,Myriad Pro,Arial,Helvetica,sans-serif;
       -moz-border-radius: 15px;
       border-radius: 15px;
       margin-left: 10px;
       margin-right: 10px;
    }
    

     

    Is there a layout I can use so that I can add a new box in the relevant file and have the boxes automatically one beneath the other?

     

    Thanks in advance for any help

  6. Not sure if this is the right forum, but I have a database that I will need to populate with a large number of rows (2000+).

     

    I have written a PHP script that uploads individual entries.

     

    Is it possible to use something like a spreadsheet where I can set out the rows/columns as they will appear in the database, and then upload in one go rather than uploading each row individually?

     

    Thanks for any observations and/or help.

  7. I've just done a Contact Me form.

     

    Once a message has been sent, I'd like to direct the user to a new page saying 'thanks for getting in touch', just so it's clear the message has been sent.

     

    What's the best function to use for that? I tried require("message.php") and include() but the two files got mixed up and all I got was a mess!

     

    Thanks in advance for any help

     

     

  8. well that's progress of sorts!

     

    Perhaps you could try using isset function to see whether the $_SESSION[] is set?

     

    http://php.net/manual/en/function.isset.php

     

    I use it like this;

     

    if (isset($_SESSION['first_name']))
        {
    
         echo '<div class="medium">Welcome';
         echo ", {$_SESSION['first_name']}!";
         echo '</div>';
      } 
    else
      {
        include ('includes/header.html');	
        echo '<h1><p>You are not logged in.</p></h1><br>';
        echo '<h2><p>If you want to access the members\' area, please register or log in.</p></h2>';
       }
    ?>
    

     

    Maybe there's something wrong with your log-in script if $_SESSION[] is not being set?

     

  9. I changed the code around and it does pretty much what I want it to now. It's probably an ugly hack though.

     

    <?php
    
    session_start();
    
    if (isset($_SESSION['first_name']))
    {	
    echo '<div id="header">';  
    include ('includes/header_loggedin.html');
    echo '</div>';  
    echo '<div id="centered">';
    
    // database connection info
    $conn = mysql_connect('','','') or trigger_error("SQL", E_USER_ERROR);
    $db = mysql_select_db('',$conn) or trigger_error("SQL", E_USER_ERROR);
    
    if(isset($_GET['search'])){
    $search = $_GET['search'];
    }
    
    //sanitise the query
    $searchTerms = trim($_GET['search']);
    $searchTerms = strip_tags($searchTerms);
    $searchTermDB = mysql_real_escape_string($searchTerms);
    
    // find out how many rows are in the table 
    $sql = "SELECT COUNT(word) FROM pro_words WHERE word LIKE '%{$searchTermDB}%'";
    $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
    $r = mysql_fetch_row($result);
    $numrows = $r[0];
    
    // number of rows to show per page
    $rowsperpage = 5;
    
    // find out total pages
    $totalpages = ceil($numrows / $rowsperpage);
    
    // get the current page or set a default
    if (isset($_GET['currentpage']) ) {
       // cast var as int
       $currentpage = $_GET['currentpage'];
    } else {
       // default page num
       $currentpage =  1;
    } // end if
    
    // if current page is greater than total pages...
    if ($currentpage > $totalpages) {
       // set current page to last page
       $currentpage = $totalpages;
    } // end if
    // if current page is less than first page...
    if ($currentpage < 1) {
       // set current page to first page
       $currentpage = 1;
    } // end if
    
    // the offset of the list, based on current page 
    $offset = ($currentpage - 1) * $rowsperpage;
    
    // resanitise the search (just in case - is it necessary?)
    $searchTerms = trim($_GET['search']);
    $searchTerms = strip_tags($searchTerms);
    $searchTermDB = mysql_real_escape_string($searchTerms);
    
    /******  make query and build the pagination links ******/
    // range of num links to show
    $range = 3;
    
    //handle attempt at null search term
    
    if (strlen($searchTerms) < 1) {
          echo '<div class="messagebox">You didn\'t specify a search term</div>';
          echo '<div class="small"><a href="search2.php"><p>Search Again?</p></a></div>';
       } else {   	
       //formulate the query
          $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
          $sql = "SELECT * FROM pro_words WHERE word LIKE '%{$searchTermDB}%' ORDER BY word LIMIT $offset, $rowsperpage";
          $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
       
    
    if (mysql_num_rows($result) < 1) {
    
     echo '<div class="messagebox">Sorry, your search term yielded no results.</div>';
        echo '<div class="small"><a href="search2.php"><p>Search Again?</p></a></div>';
    
    } else {
    //display results	
    	echo '<div class="messagebox">Here are your search results.</div>';		
    
    	while ($list = mysql_fetch_array($result)) {
    // echo data
    echo '<div class="small"><a href="word.php?w=' . $list['word'] . '">' . $list['word'] . '</a></div>';  
      } 
    // if not on page 1, don't show back links
    if ($currentpage > 1) {
       // show << link to go back to page 1
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
       // get previous page num
       $prevpage = $currentpage - 1;
       // show < link to go back to 1 page
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
    } // end if 
    
    // loop to show links to range of pages around current page
    for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
       // if it's a valid page number...
       if (($x > 0) && ($x <= $totalpages)) {
          // if we're on current page...
          if ($x == $currentpage) {
             // 'highlight' it but don't make a link
             echo " <b>$x</b> ";
          // if not current page...
          } else {
             // make it a link
             echo " <a href='{$_SERVER['PHP_SELF']}?search=$search&currentpage=$x'>$x</a> ";
          } // end else
       } // end if 
    } // end for
                     
    // if not on last page, show forward and last page links        
    if ($currentpage != $totalpages) {
       // get next page
       $nextpage = $currentpage + 1;
        // echo forward link for next page 
       echo " <a href='{$_SERVER['PHP_SELF']}?search=$search&currentpage=$nextpage'>></a> ";
       // echo forward link for lastpage
       echo " <a href='{$_SERVER['PHP_SELF']}?search=$search&currentpage=$totalpages'>>></a> ";
    }
    } 
    }// end if
    /****** end build pagination links ******/
    } else {
    include ('includes/header.html');
    echo '<div class="box">If you want to access this part of the site, please register or log-in.</div>';
      }
    ?>
    

     

    So I solved it. Asking a question can be instructional.

  10. Much the same but you can use the mysql_num_rows function too.

     

    e.g.

     

    $result = mysql_query($query) or die(mysql_error());
    $num_rows = mysql_num_rows($result);
    

     

    then echo $num_rows where you want the count to go.

  11. I use this on my site. YOu'd need to adapt it for your own of course.

     

    <?php
      session_start();
    
        // check session variable
    
      if (isset($_SESSION['first_name']))
      {
    
             echo 'Welcome';
     echo ", {$_SESSION['first_name']}!";
            
    
    
    

     

    just change the $_SESSION[] from first_name to whatever you use on your db.

     

    btw, I use the isset is to ensure that someone is logged on.

     

    The useful code as far as your query goes is {$_SESSION['first_name]}.

     

    hope that helps

  12. Sure, here it is.

     

    <?php
    
    session_start();
    
    if (isset($_SESSION['first_name']))
    {	
    echo '<div id="header">';  
    include ('includes/header_loggedin.html');
    echo '</div>';  
    echo '<div id="centered">';
    
    // database connection info
    $conn = mysql_connect('localhost','vvv',mmmm') or trigger_error("SQL", E_USER_ERROR);
    $db = mysql_select_db('db',$conn) or trigger_error("SQL", E_USER_ERROR);
    
    if(isset($_GET['search'])){
    $search = $_GET['search'];
    }
    
    // find out how many rows are in the table 
    
    if (isset($_GET['search'])) {
    $searchTerms = trim($_GET['search']);
    $searchTerms = strip_tags($searchTerms); // remove any html/javascript.
       
    if (strlen($searchTerms) < 1) {
          $error[] = '<div class="messagebox">You didn\'t specify a search term</div></div>';
          $error[] = '<div class="small"><a href="search.php"><p>Search Again?</p></a></div>';
       } else {
          $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
       }
      
    $error=array();
    
    $sql = "SELECT COUNT(*) FROM pro_words WHERE word LIKE '%{$searchTermDB}%'";
    $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
    $r = mysql_fetch_row($result);
    $numrows = $r[0];
    }
    // number of rows to show per page
    $rowsperpage = 5;
    // find out total pages
    $totalpages = ceil($numrows / $rowsperpage);
    
    // get the current page or set a default
    if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
       // cast var as int
       $currentpage = (int) $_GET['currentpage'];
    } else {
       // default page num
       $currentpage = 1;
    } // end if
    
    // if current page is greater than total pages...
    if ($currentpage > $totalpages) {
       // set current page to last page
       $currentpage = $totalpages;
    } // end if
    // if current page is less than first page...
    if ($currentpage < 1) {
       // set current page to first page
       $currentpage = 1;
    } // end if
    
    // the offset of the list, based on current page 
    $offset = ($currentpage - 1) * $rowsperpage;
    
    // get the info from the db 
    if (isset($_GET['search'])) {
    $searchTerms = trim($_GET['search']);
    $searchTerms = strip_tags($searchTerms); // remove any html/javascript.
       
    if (strlen($searchTerms) < 1) {
          $error[] = '<div class="messagebox">You didn\'t specify a search term</div></div>';
          $error[] = '<div class="small"><a href="search2.php"><p>Search Again?</p></a></div>';
       } else {
          $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
       }
    
    $sql = "SELECT * FROM pro_words WHERE word LIKE '%{$searchTermDB}%' ORDER BY word LIMIT $offset, $rowsperpage";
    $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
    $num_rows = mysql_num_rows($result);
    
    //display results
    
    if (mysql_num_rows($result) < 1) {
             $error[] = '<div class="messagebox">Sorry, your search term yielded no results.</div>';
             $error[] = '<div class="small"><a href="search.php"><p>Search Again?</p></a></div>';
          } else {
          echo '<div class="messagebox">Here are your search results.</div>';		
    
    	while ($list = mysql_fetch_array($result)) {
       // echo data
    echo '<div class="small"><a href="word.php?w=' . $list['word'] . '">' . $list['word'] . '</a></div>';  
      } //end while	
    
      			}	
    	}	
    
    echo (count($error) > 0)?"<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; 
    
    /******  build the pagination links ******/
    // range of num links to show
    $range = 3;
    
    // if not on page 1, don't show back links
    if ($currentpage > 1) {
       // show << link to go back to page 1
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
       // get previous page num
       $prevpage = $currentpage - 1;
       // show < link to go back to 1 page
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
    } // end if 
    
    // loop to show links to range of pages around current page
    for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
       // if it's a valid page number...
       if (($x > 0) && ($x <= $totalpages)) {
          // if we're on current page...
          if ($x == $currentpage) {
             // 'highlight' it but don't make a link
             echo " [<b>$x</b>] ";
          // if not current page...
          } else {
             // make it a link
             echo " <a href='{$_SERVER['PHP_SELF']}?search=$search&currentpage=$x'>$x</a> ";
          } // end else
       } // end if 
    } // end for
                     
    // if not on last page, show forward and last page links        
    if ($currentpage != $totalpages) {
       // get next page
       $nextpage = $currentpage + 1;
        // echo forward link for next page 
       echo " <a href='{$_SERVER['PHP_SELF']}?search=$search&currentpage=$nextpage'>></a> ";
       // echo forward link for lastpage
       echo " <a href='{$_SERVER['PHP_SELF']}?search=$search&currentpage=$totalpages'>>></a> ";
    } // end if
    /****** end build pagination links ******/
    } else {
    include ('includes/header.html');
    echo '<div class="box">If you want to access this part of the site, please register or log-in.</div>';
      }
    ?>
    

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.