#1 (permalink)  
Old 07-06-2008, 07:24 PM
Registered User
 
Join Date: Jul 2004
Posts: 247
php sql not working in for loop

I don't understand why the sql isn;t working in the for loop:
PHP Code:
<?php
require_once "Paginated.php";
require_once 
"DoubleBarLayout.php";
/******************************************************************************/
//connection
$con mysql_connect("","root","");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }

mysql_select_db("kjv"$con);

/******************************************************************************/
$searchThese $_GET['searchthese'];
$searchTheseArr $searchThese;
$searchTheseArr=str_replace(":"""$searchTheseArr);
$searchTheseArr=str_replace(";"""$searchTheseArr);
$searchTheseArr=str_replace(","""$searchTheseArr);
$searchTheseArr=str_replace("."""$searchTheseArr);
$searchTheseArr=str_replace("?"""$searchTheseArr);
//common words
$searchTheseArr=str_replace("shall"""$searchTheseArr);
$searchTheseArr=str_replace("shalt"""$searchTheseArr);
$searchTheseArr explode(" "$searchTheseArr);

function 
bookTitles(){
       
$booktitlesarray = array();
    
$sql="SELECT * FROM bible WHERE 1=1 AND";
    
$j=0;
    
//to sort out all words with length less than 4 like AND, OR, BUT...
    
for ($i=0$i count($searchTheseArr); $i++){
        if(
strlen($searchTheseArr[$i]) > 4){
            
$newSearchTheseArr[$j] = $searchTheseArr[$i];
    
//j renumbers the words of length 4 and up        
            
$j++;
        }
    }
    
//sql searches only length 4 and up
    
for ($j=0$j count($newSearchTheseArr); $j++){
        
$sql.=" CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
        if(
$j!=count($newSearchTheseArr)-1){
            
$sql.=" +";
        }else{
    
//removes the OR from the last line and replaces with the following    
            
$sql.= " > 3";
        }
    }
   
$sql .= " ORDER BY id";
   echo 
$sql;
   
$result mysql_query($sql);
   if(
$result){
      while(
$row mysql_fetch_assoc($result)){
          
$booktitlesarray[] = "<span class='goToBookChapter' style='font-weight: bold;'>".$row['book_title']." ".$row['chapter'].":".$row['verse']."</span><br />".$row['text_data']."<br />";
      }
   }
   return 
$booktitlesarray;


    
$names bookTitles(); 

    
$page $_GET['page'];
    
//constructor takes three parameters
    //1. array to be paged
    //2. number of results per page (optional parameter. Default is 10)
    //3. the current page (optional parameter. Default  is 1)
    
$pagedResults = new Paginated($names10$page);
    echo 
"<div>\n";
    while(
$row $pagedResults->fetchPagedRow()){
        echo 
"<span>{$row}</span><br />\n";
    }
    echo 
"</div>\n";
    
//mysql_close($con);
    //important to set the strategy to be used before a call to fetchPagedNavigation
    
$pagedResults->setLayout(new DoubleBarLayout());
    echo 
$pagedResults->fetchPagedNavigation();
    
mysql_close($con);
?>
When I echo $searchThese right after the $searchThese = $_GET['searchthese']; it shows.

The intention of the first for loop is to sort out the small words of less than 4 letters.
Reply With Quote

  #2 (permalink)  
Old 10-17-2008, 09:14 PM
Registered User
 
Join Date: Oct 2007
Location: Worcester, U.K.
Posts: 5
Quote:
Originally Posted by gilgalbiblewhee View Post
I don't understand why the sql isn;t working in the for loop:
$booktitlesarray[] = "<span class='goToBookChapter' style='font-weight: bold;'>".$row['book_title']." ".$row['chapter'].":".$row['verse']."</span><br />".$row['text_data']."<br />";
Hi,
you've not specified an index for $booktitlesarray,

Declare a variable and initialise it as "0", like so : $bookcount = 0;

Then use this as the index of the array and increment it; $booktitlesarray[$bookcount++] = "<span... etc.

Like so;

if($result){
$bookcount = 0;
while($row = mysql_fetch_assoc($result)){
$booktitlesarray[$bookcount++] = "<span class='goToBookChapter' style='font-weight: bold;'>".$row['book_title']." ".$row['chapter'].":".$row['verse']."</span><br />".$row['text_data']."<br />";
}
}
Reply With Quote
  #3 (permalink)  
Old 10-18-2008, 09:14 AM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,438
What, exactly, is the for loop not doing. I don't know what you mean by "isn't working".

BTW - @cphale - you don't need to specify an index when adding to an array in PHP. If you leave the index empty, PHP automatically chooses the next available index in the array.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Testing php application with apache lax Programming and Scripting 1 10-17-2008 09:03 PM
PHP linking to bottom of page.. yuppicide Programming and Scripting 5 02-20-2008 04:28 PM
WHERE in sql wrong in PHP gilgalbiblewhee Programming and Scripting 7 11-07-2007 04:45 PM
PHP cookies and multiple values curtiss Programming and Scripting 1 11-07-2007 11:39 AM
Using PHP, SQL and Variables curtiss Programming and Scripting 4 04-16-2005 10:20 AM


All times are GMT -5. The time now is 06:06 AM.

 
Bitrix
Clicky Web Analytics
CloudContacts
Maxtango


Subscribe to our feed | add to myYahoo!

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
© 1997-2007 HTMLCenter