7 Replies - 817 Views - Last Post: 21 March 2013 - 10:30 AM Rate Topic: -----

#1 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

online address book

Posted 21 March 2013 - 01:01 AM

when i click on the link A i must get the details starting from letter a.. i know the query i have to use LIKE statement but what i want is how to check the condition which alphabet is clicked i mean which link is clicked using the IF condition ? and what changes has to be done in my code..






<html>
<body>
<?
$conn = mysql_connect("localhost","root","");
mysql_select_db("sangeetha",$conn);
$sql = "select * from persondet";
$result = mysql_query($sql,$conn);
if(letter=='a')
{
   $query = "select * from persondet where Name LIKE 'a%'";
   }
   else if(letter=='b')
   {
     $query = "select * from persondet where Name LIKE 'b%'";
	  }
	 else if(letter=='c')
   {
     $query = "select * from persondet where Name LIKE 'c%'";
	  } 
	  else if(letter=='d')
   {
     $query = "select * from persondet where Name LIKE 'd%'";
	  }
	  else if(letter=='e')
   {
     $query = "select * from persondet where Name LIKE 'e%'";
	  }
mysql_close($conn)
?>
</body>
</html>




<html>
<body>

<form action="persondet.html" method="post">
Search <input type="search" name="search"  />
<input type="submit" value="ADD" />
<table border="1">
<tr>
<td><a href="index.php?letter=a">A</a></td>
<td><a href="index.php?letter=b">B</a></td>
<td><a href="index.php?letter=c">C</a></td>
<td><a href="index.php?letter=d">D</a></td>
<td><a href="index.php?letter=e">E</a></td>
<td><a href="index.php?letter=f">F</a></td>
<td><a href="index.php?letter=g">G</a></td>
<td><a href="index.php?letter=h">H</a></td>
<td><a href="index.php?letter=i">I</a></td>
<td><a href="index.php?letter=j">J</a></td>
<td><a href="index.php?letter=k">K</a></td>
<td><a href="index.php?letter=l">L</a></td>
<td><a href="index.php?letter=m">M</a></td>
<td><a href="index.php?letter=n">N</a></td>
<td><a href="index.php?letter=o">O</a></td>
<td><a href="index.php?letter=p">P</a></td>
<td><a href="index.php?letter=q">Q</a></td>
<td><a href="index.php?letter=r">R</a></td>
<td><a href="index.php?letter=s">S</a></td>
<td><a href="index.php?letter=t">T</a></td>
<td><a href="index.php?letter=u">U</a></td>
<td><a href="index.php?letter=v">V</a></td>
<td><a href="index.php?letter=w">W</a></td>
<td><a href="index.php?letter=x">X</a></td>
<td><a href="index.php?letter=y">Y</a></td>
<td><a href="index.php?letter=z">Z</a></td>
</tr>
</table>
</form>

</body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: online address book

#2 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: online address book

Posted 21 March 2013 - 01:10 AM

I've included a couple snippets for you, I hope they help

for (i=97; i<=122; i++) { echo(chr(i)); }



you can use something like that to create your link list and save yourself having to type each link by hand.

and
$search = $_GET['letter'];



is what you'll be looking for to gather the letter being submitted. Cram that into a query and you should be fine.


Also, the standard response for people building queries like this is to look into 'paramaterized queries' which is more so on the safe side of things as far as SQL injection goes. Do some searches on this and you'll see how it can be a good thing when you move into authentication systems and the like.
Was This Post Helpful? 0
  • +
  • -

#3 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

Re: online address book

Posted 21 March 2013 - 01:29 AM

is for loop is a must sir ? what about the if condition in the program is it right or wrong ?
Was This Post Helpful? 0
  • +
  • -

#4 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

Re: online address book

Posted 21 March 2013 - 03:00 AM

Sir this code is working proper and thanks for your guidance sir .. but i just want to reduce the line of code.. guide me how to do that because the code is getting repeated if i can write any function for fetching the data pls let me know


<?
$search = $_GET['letter'];
$conn = mysql_connect("localhost","root","");
mysql_select_db("sangeetha",$conn);
$sql = "select * from persondet";
$result = mysql_query($sql,$conn);

if($search == 'a')
{
   $query = "select * from persondet where Name LIKE 'a%'";
   $result=mysql_query($query,$conn);
   echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>

</tr>";
   while($newArray=mysql_fetch_array($result))
{
	
	
	echo "<tr>";
	echo "<td>" . $newArray['id'] . "</td>";
	echo "<td>" . $newArray['Name'] . "</td>";
	echo "<td>" . $newArray['Address'] . "</td>";
	echo "<td>" . $newArray['Phone'] . "</td>";
	echo "</tr>";
	/*$id = $newArray[id];
	$name = $newArray[Name];
	$address = $newArray[Address];
	$phone = $newArray[Phone];
	echo $id,$name,$address,$phone ."<br>";*/
}

}
  
if($search == 'b')
   {
     $query = "select * from persondet where Name LIKE 'b%'";
	 $result=mysql_query($query,$conn);
	  echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>

</tr>";
   while($newArray=mysql_fetch_array($result))
{
	
	echo "<tr>";
	echo "<td>" . $newArray['id'] . "</td>";
	echo "<td>" . $newArray['Name'] . "</td>";
	echo "<td>" . $newArray['Address'] . "</td>";
	echo "<td>" . $newArray['Phone'] . "</td>";
	echo "</tr>";
	
}
}

if($search == 'c')
   {
     $query = "select * from persondet where Name LIKE 'c%'";
	 $result=mysql_query($query,$conn);
	  echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>

</tr>";
   while($newArray=mysql_fetch_array($result))
{
	echo "<tr>";
	echo "<td>" . $newArray['id'] . "</td>";
	echo "<td>" . $newArray['Name'] . "</td>";
	echo "<td>" . $newArray['Address'] . "</td>";
	echo "<td>" . $newArray['Phone'] . "</td>";
	echo "</tr>";
	
	
}
	  } 
	  



Was This Post Helpful? 0
  • +
  • -

#5 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: online address book

Posted 21 March 2013 - 08:47 AM

looking good, youll want to use something like below for your $query line. Its called 'string concatenation'

$query = "select * from persondet where Name LIKE '{$search}%'";



if you surround the variable name with { } inside of a string, it will actually cram your variable value in there, it's a short hand version of something like

$query = "select * from persondet where Name LIKE '" . a . "%'";

or 

$newstring = $firststring . $secondstring;





however, as I said in the first post, also look at the link below. In circumstances like your current situation, this will work fine, but consider the following


$user = input value (  ' OR 'test'='test' )
$pass = same scenario
$query = "SELECT * FROM tblUsers WHERE userName='{$user}' AND userPassword='{$pass}';




SQL will interpret both halves of the query as TRUE and spit out access rights without having input correct credentials (too early to pull this stuff back into my mind, but that is the jist of SQL injection, the input above is likely not to actually bypass a login query) and can cause insecurities in certain circumstances.


http://php.net/manua...-statements.php

and finally, the for loop is not a must, but you're on the right path with minimizing code(or hard typed info in general) and it wouldn't hurt, check out the code below for a bit more info on how this could work.

for (i=97; i<=122; i++) { 
'Every letter in computer world is represented by a number value also
'a is represented by the number 97
'z is represented by the number 122
'so this loop, in conjunction with the chr() function will output every letter between a and z
?><a href="mylink?letter=<?php=chr(i)?>"><?php=chr(i)?></a><? 
}




this will actually generate your links for you, and is way easier to maintain in the situation that say you decide you want to make every lower case letter a capital letter instead, you change the two instances of chr(i) to strtoupper(chr(i) instead of having to go down the entire list changing a-A b-B c-C, etc.

This post has been edited by Takk: 21 March 2013 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#6 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

Re: online address book

Posted 21 March 2013 - 08:56 AM

sir i want to reduce the lines of code than the query sir
Was This Post Helpful? -1
  • +
  • -

#7 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: online address book

Posted 21 March 2013 - 10:05 AM

At a quick glance, it you follow the few things I've posted, you're looking at somewhere around 40 lines total. For a-c in both or your examples above you're looking at something over 900, I'd say we're doing pretty well so far to reduce the number of lines :)

This post has been edited by Takk: 21 March 2013 - 10:06 AM

Was This Post Helpful? 0
  • +
  • -

#8 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

Re: online address book

Posted 21 March 2013 - 10:30 AM

ok sir i will try once.. thank you so much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1