Help with this code...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pido
Forum Commoner
Posts: 35
Joined: Mon Jan 12, 2004 8:03 am

Help with this code...

Post by pido »

hi i have this code

Code: Select all

<?php
// Lists the letters of the alphabet with
// links associated with each letter
	  print "\t\t<a href="?alpha=0" class="alphalink">0-9</a>\n";
	  for($i=65;$i<=90;$i++){
		  $x = chr($i);
		  $alphalink = "?alpha=".$x;
		  print "\t\t\t<a href="$alphalink" class="alphalink">$x</a>\n";
	  }
?>
and i want those links redirect me to certain book title, this my next code

Code: Select all

<?php
if($_GET["alpha"]){
	db_connect(); // This is function that i made for connecting to db server
	$id = $HTTP_GET_VARS["alpha"];
	$query = "SELECT PK_bookID,bookTitle,yearPublished,pages,ISBN FROM tbl_book WHERE bookTitle LIKE '$id%' GROUP BY bookTitle";
	$result = mysql_query($query) or die(mysql_error());
//	if(!mysql_fetch_row($result)){
//		print "<td><font size="-1" color="grey"><i>no title found!</i></font>";
//		print "<td><font size="-1" color="grey"><i>n/a</i></font>";
//		print "<td><font size="-1" color="grey"><i>n/a</i></font>";
//		print "<td><font size="-1" color="grey"><i>n/a</i></font>";
//		exit;
//	}
	while ($alpha_rows = mysql_fetch_array($result)){
	?>
	<tr>
		<td><a href="show.php?title=<? echo "$alpha_rows[0]"; ?><? echo "$book_rows[0]"?>"><? echo "$alpha_rows[1]"; ?></a></td>
		<td><? echo "$alpha_rows[2]"; ?></td>
		<td><? echo "$alpha_rows[3]"; ?></td>
		<td><? echo "$alpha_rows[4]"; ?></td>
	</tr>
	<?
	}
}
?>
The reason i put comments there, its because when i put that code, its worked tho, but it can't retrive the first record, but for 2nd until end of records are displayed.

Question are:

1. How can i display a "NULL" value?? i mean if i do query and i get NULL value and i want to display, lets say i want to display "No Book Title Fund!"
2. And for the link show.php?title=0 i want it link to my records which is bookTitle that has 0 (zero) in it, how do i do that??

thnx...
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

When you call mysql_fetch_row to see if there are any results you move the pointer to the second result for when you call it next to display the results.

Use mysql_num_rows() to see how many results have been returned. You can use this to answer your first question.

When you use LIKE in the sql statment it checks that character, therefor if you do:

Code: Select all

LIKE '$id%'
It will match the 0. Just create a link that you said above.
User avatar
pido
Forum Commoner
Posts: 35
Joined: Mon Jan 12, 2004 8:03 am

Post by pido »

thnx for the reply,

Code: Select all

mysql_num_rows()
is worked, that answered my first question, thnx kettle_drum

But for

Code: Select all

$query = "SELECT PK_bookID,bookTitle,yearPublished,pages,ISBN FROM tbl_book WHERE bookTitle LIKE '$id%' GROUP BY bookTitle";
that i put in my $query, its only give me a return value of characters.
How do i make a query that can return records that only has '0-9' (number) <-- in the first sentence, e.g "20 years" or "911 rescue"...??
oh and i did put a link for

Code: Select all

show.php?title=0
User avatar
pido
Forum Commoner
Posts: 35
Joined: Mon Jan 12, 2004 8:03 am

Post by pido »

hmm.. just remembered i have to use

Code: Select all

BETWEEN '0' AND '9'
:D

oh and do you know how to make query for searching?? i made one, but i can't get any search for seperated words, "search this" but for "search" <-- single word, its work fine... any idea??
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

For searching it depends on how you want to search.... but you would use the LIKE function and look for where the field is like what your searching for

to find both words in the order you specify

Code: Select all

LIKE '%search%this%'



to just check if they are both there

Code: Select all

LIKE '%search' AND LIKE '%this%'
or to check if just one of them is there

Code: Select all

LIKE '%search' OR LIKE '%this%'
Post Reply