Forum Moderators: coopster

Message Too Old, No Replies

Link ID from database not showing in search page

Can some please tell me why the links to the articles that display is blank

         

ecnaralc

5:26 am on May 18, 2007 (gmt 0)

10+ Year Member



http://www.example.com/test/view.php?id=
is what gets displayed in address bar.The search finds all records searched for in DB - just no ID when I click on link.
Search page info:

<?php
$hostname_logon = "localhost" ;
$database_logon = "tester" ;
$username_logon = "root" ;
$password_logon = "" ;
$connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ( "Unabale to connect to the database" );
mysql_select_db($database_logon) or die ( "Unable to select database!" );
$limit = 15;
$var = @$_GET['q'] ;
$trimmed = trim($var);
$trimmed_array = explode(" ",$trimmed);
if ($trimmed == "") {
$resultmsg = "<p>Search Error</p><p>Please enter a search...</p>" ;
}
if (!isset($var)){
$resultmsg = "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;
}
foreach ($trimmed_array as $trimm){
$query = "select * from cmsarticles WHERE id LIKE \"%$trimm%\" OR title LIKE \"%$trimm%\" OR tagline LIKE \"%$trimm%\" OR thearticle LIKE \"%$trimm%\" ORDER BY id DESC" ;
$numresults=mysql_query ($query);
$row_num_links_main =mysql_num_rows ($numresults);
$s=0;
if (isset($_GET['s'])) $s = (int)$_GET['s'];

$query .= " LIMIT $s,$limit" ;
do{
$adid_array[] = $row[ 'id' ];
}while( $row= mysql_fetch_array($numresults));
} //end foreach

if($row_num_links_main == 0 && $row_set_num == 0){
$resultmsg = "<p>Search results for:" . $trimmed ."</p><p>Sorry, your search returned zero results</p>" ;
}
$tmparr = array_unique($adid_array);
$i=0;
foreach ($tmparr as $v) {
$newarr[$i] = $v;
$i++;
}

?>
<form action="search.php" method="get" name="search">
<div align="center">
<input name="q" type="text" value=" <?php echo $q;?> " size="15">
<input name="search" type="submit" value="Search">
</div>
</form>
<p>
<?php
if( isset ($resultmsg)){
echo $resultmsg;
exit();
}else{
echo "Search results for: " . $var;
}

foreach($newarr as $value){
// EDIT HERE and specify your table and field names for the SQL query
$query_value = "SELECT * FROM cmsarticles WHERE id = '$value'";
$num_value=mysql_query ($query_value);
$row_linkcat= mysql_fetch_array ($num_value);
$row_num_links= mysql_num_rows ($num_value);
$titlehigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'id' ] );
$linkhigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'title' ] );
$linkdesc = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'tagline' ] );
$linklow = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'thearticle' ] );

foreach($trimmed_array as $trimm){
if($trimm!= 'b' ){
$titlehigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $titlehigh);
$linkhigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $linkhigh);
$linkdesc = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $linkdesc);
$linklow = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $linklow);
}
//end highlight

?>
</p>
<p>
<?php
//BEGINNING OF 2ND SCRIPT
$finalresult = mysql_query($query) or die('Query failed: ' . mysql_error());
$numofrows = mysql_num_rows($finalresult);
for($i = 0; $i < $numofrows; $i++) {
$frow = mysql_fetch_array($finalresult); //get a row from our result set
if($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#F0F0F0\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo '<p> <a href="view.php?id='.$frow['id'].'">';
echo $frow['title'];
echo "</a> </p></TR>\n";
echo $frow['tagline'];
echo ' <a href="view.php?id='.$frow['id'].'">more';
}
echo "</TABLE>\n";
//END OF 2ND SCRIPT
?>
</p>
<?php
} //end foreach $trimmed_array
if($row_num_links_main > $limit){
// next we need to do the links to other search result pages
if ($s>=1) { // do not display previous link if 's' is '0'
$prevs=($s-$limit);
echo "<div align='left'><a href='$PHP_SELF?s=$prevs&q=$var&catid=$catid'>Previous " .$limit. "</a></div>";
}
// check to see if last page
$slimit =$s+$limit;
if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) {
// not last page so display next link
$n=$s+$limit;
echo "<div align='right'><a href='$PHP_SELF?s=$n&q=$var&catid=$catid'>Next " .$limit. "</a></div>";
}
}
} //end foreach $newarr
?>

[edited by: dreamcatcher at 2:40 pm (utc) on May 24, 2007]
[edit reason] Exemplified url. [/edit]

mcibor

6:42 am on May 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you mean, that
echo '<p> <a href="view.php?id='.$frow['id'].'">';
echo $frow['title'];
echo "</a> </p></TR>\n";
echo $frow['tagline'];
echo ' <a href="view.php?id='.$frow['id'].'">more';

$frow['id'] is empty?
And what about $frow['title']?
Is it empty as well?

If so, then you may want to check the sql.

Moreover, do you really need your first id empty?

$numresults=mysql_query ($query);
$row_num_links_main =mysql_num_rows ($numresults);
$s=0;
if (isset($_GET['s'])) $s = (int)$_GET['s'];

$query .= " LIMIT $s,$limit" ;
do{
$adid_array[] = $row[ 'id' ];
}while( $row= mysql_fetch_array($numresults));

On the first loop the $row['id'] is unset. Maybe that's what causing your problems. You shouldn't loop with do...while, only while...:

$numresults=mysql_query ($query);
$row_num_links_main =mysql_num_rows ($numresults);
$s=0;
if (isset($_GET['s'])) $s = (int)$_GET['s'];

$query .= " LIMIT $s,$limit" ;
while($row=mysql_fetch_array($numresults)){
$adid_array[] = $row[ 'id' ];
}

Hope this helps

Michal

BTW welcome to Webmasterworld!
and try not to post so much irrelevant code (eg. pagination)

ecnaralc

12:42 am on May 24, 2007 (gmt 0)

10+ Year Member



Hello...

Thanks for your help.. but view.php?id= is still not showing any id
when I place the cursor over the link displayed in search result all that shows is view.php?id=

Your question: Moreover, do you really need your first id empty? No I want this to display id so it can be used by visitor

$frow['id'] is empty?
$frow['title'] does display the title
$frow['tagline'] also displays data

I took out $frow['id'].'">more'; but id remains empty
I am not sure why the id is empty or what to do next... any other hints would be appreciated

mcibor

6:40 am on May 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I understand, that the query that is not working well is:

$query = "SELECT * FROM cmsarticles WHERE id LIKE \"%$trimm%\" OR title LIKE \"%$trimm%\" OR tagline LIKE \"%$trimm%\" OR thearticle LIKE \"%$trimm%\" ORDER BY id DESC LIMIT $s,$limit";
?

the only other option I could think of is to print out whole $frow and check manually if there is an id inside it or not, and what association does it have:


/* It will be just temporary, to determine what is wrong */

$finalresult = mysql_query($query) or die('Query failed: ' . mysql_error());
$numofrows = mysql_num_rows($finalresult);
$frow = mysql_fetch_array($finalresult);

echo "Query: $query<br><br>frow:";
print_r($frow);
die('<br><br>Now you can check if the data is correct');

This should help you to check it all. Cause when I look through your code it seems OK.
Try to echo that sql and its results, that can be the only problem I can think of...

Regards
Michal

ecnaralc

1:18 pm on May 24, 2007 (gmt 0)

10+ Year Member



Thanks Michal.
I have done what you suggested.
I made a new DB with only one record in it. Same format as original.
Here is the reult.
I have edited yards of words out.

Search results for: welcome

Query: select * from cmsarticles WHERE id LIKE "%welcome%" OR title LIKE "%welcome%" OR tagline LIKE "%welcome%" OR thearticle LIKE "%welcome%" ORDER BY id DESC LIMIT 0,15

frow:Array ( [0] => 1 [ID] => 1 [1] => Welcome to [title] => Welcome to [2] => If you've
[tagline] => If you've read
[3] => 1 [section] => 1 [4] => For demonstration . [5] => [print] => [6] => [images] => [7] => [sectionname] => [8] => 0000-00-00 00:00:00 [timestamp] => 0000-00-00 00:00:00 )

Now you can check if the data is correct

It does show everything in that one record....
It is perplexing to me...
Wayne

whoisgregg

1:49 pm on May 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like "id" is actually uppercase "ID." That could be the difference. Try:

echo $frow['ID'];

ecnaralc

2:06 pm on May 24, 2007 (gmt 0)

10+ Year Member



Thank you very much whoisgregg...
I have been sitting here for ten minutes laughing my head off..
It works like a charm... totally absolutely perfect...

I have another good one like this.. but I will post it soon... perhaps you may know the answer just by looking ...
Thanks again... to this site existance...

mcibor

7:13 am on May 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just wonder why the query didn't return errors:
SELECT * FROM cmsarticles WHERE id=2

Here id was small letters. But I'm glad Greg found it!

Regards
Michal

ecnaralc

12:18 pm on May 25, 2007 (gmt 0)

10+ Year Member



Thanks also to you Michal...
I changed all of the id to ID in one swoop, I thought it would save time in the long run.
Possibly I have been lucky to this time using lowercase id...
It has been a good lesson, to make sure that everything matches...
Regards
Wayne