Jump to content

[SOLVED] my A-Z links are not showing


yobo

Recommended Posts

here is my page with the letters on and the code is shown

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<a href="searchByList.php?index=#">#</a>

<a href="searchByList.php?index=A">A</a>

<a href="searchByList.php?index=B">B</a>

<a href="searchByList.php?index=C">C</a>

<a href="searchByList.php?index=D">D</a>

</body>

</html>

 

and this is the reciving script

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<?php

include 'db.inc.php';

$colname = "index";

 

if (isset($_GET['index'])) {

  $colname = (get_magic_quotes_gpc()) ? $_GET['index'] : addslashes($_GET['index']);

}

 

if ($colname == "A"){

$colname = preg_replace("/[^0-9]/", "" , $colname);

 

}

$query_rsSearchList = "SELECT * FROM name WHERE name1 LIKE '$colname%' AND id = id ORDER BY name1 ASC ";

 

?>

</body>

</html>

 

but for some reasson no data is being outputted back to the broswer it is comming up with a blank page, i know i need an echo statement in there but i don't know where to add it and what variable to add

Link to comment
https://forums.phpfreaks.com/topic/40924-solved-my-a-z-links-are-not-showing/
Share on other sites

that's because you never execute the query or echo anything.  if you're using MySQL for example, you would have to add:

 

$result = mysql_query($query_rsSearchList);

 

then you'd have to output the results:

 

while($row = mysql_fetch_assoc($result)){
  echo $row['colname1'] . " " . $row['colname2'] . "" . $row['colname3'] . "<br>";
}

 

and replace the colname stuff with the names of the columns in your table.  also you're going to have problems with your SQL most likely.  try changing it to:

 

$query_rsSearchList = "SELECT * FROM name WHERE name1 LIKE '$colname%' ORDER BY name1 ASC "; 

 

where id=id isn't going to do anything unless you have a column named id, and you have the string "id" in it for some bizarre reason.  but even then you would need it to read: where id='id'

 

also I don't get the point of that preg_replace()...

ok i have got it to display the page and the data the only problem is that when i click on the letter a it pulls all the data from the certain table and not just the names that begin with the letter a here is my updated code

 

<?php

include 'db.inc.php';

 

$colname = "A";

 

if (isset($_GET['index'])) {$colname = (get_magic_quotes_gpc()) ? $_GET['index'] : addslashes($_GET['index']);}

 

if ($colname == "A")

 

$sql = "SELECT * FROM name WHERE name1 LIKE '$a%' ORDER BY name1 ASC ";

        $result = mysql_query($sql);

       

      while($row = mysql_fetch_assoc($result)){

  echo $row['name1'] . " " . $row['age'] . "<br>";

}

 

    mysql_free_result($result);

 

?>

use the LEFT function in mysql.

 

<?php
include 'db.inc.php';

$colname = "A";

if (isset($_GET['index'])) {$colname = (get_magic_quotes_gpc()) ? $_GET['index'] : addslashes($_GET['index']);}

$sql = "SELECT * FROM name WHERE LEFT(`name1`,1)='{$colname}' ORDER BY name1 ASC ";
        $result = mysql_query($sql);
         
      while($row = mysql_fetch_assoc($result)){
  echo $row['name1'] . " " . $row['age'] . "
";
}

    mysql_free_result($result);

?>

thanks Project fear for helping me but i have another problem for some reasson the results appear all on one line how do i get it so that the appear below each other in a list

      while($row = mysql_fetch_assoc($result)){

  echo $row['name1'] . " " . $row['age'] . ";

}

but the results don't go onto a new line

 

<?php

while($row = mysql_fetch_assoc($result)){
  echo $row['name1'] . " " . $row['age'] ."<br>";
}

?>

 

The

<br>

tag is needed to insert a line break

 

 

i have done what you said above and it is still displaying on one line i have been working to try and get it on seperate lines for over 4+ hours and still can't get it to work

hmmmmmmmmmmmmmmmmm... that br tag should have forced a new line.

 

 

just for fun, add about 2 or 3 more br tags and see if that does anything

 

 

<?php

while($row = mysql_fetch_assoc($result)){
  echo $row['name1'] . " " . $row['age'] ."<br><br><br>";
}

?>

 

 

This SHOULD put 2 blank line between each row of data.

still dose not seem to want to work

 

here is all my code for your to see

 

my db.inc.php code:

 

PHP Code:

<?php

//database connection settings

$dbcon = @mysql_connect('localhost', 'root', 'root');

if (!$dbcon) {

    exit('Error Connecting To The Site Database.');

}

if (!@mysql_select_db('letters', $dbcon)) {

    exit('Error opening the site database.');

}

?>

my indiex.php code:

 

PHP Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<a href="searchByList.php?index=#">#</a>

<a href="searchByList.php?index=A">A</a>

<a href="searchByList.php?index=B">B</a>

<a href="searchByList.php?index=C">C</a>

<a href="searchByList.php?index=D">D</a>

</body>

</html>

 

my searchByList.php code:

 

PHP Code:

<?php

include 'db.inc.php';

 

$colname = "A";

 

if (isset($_GET['index'])) {$colname = (get_magic_quotes_gpc()) ? $_GET['index'] : addslashes($_GET['index']);}

 

$sql = "SELECT * FROM name WHERE LEFT(`name1`,1)='{$colname}' ORDER BY name1 ASC ";

        $result = mysql_query($sql);

         

      while($row = mysql_fetch_assoc($result)){

  echo $row['name1'] . " " . $row['age'] . "

";

}

 

    mysql_free_result($result);

 

?>

 

<?php

$colname = "B";

 

if (isset($_GET['index'])) {$colname = (get_magic_quotes_gpc()) ? $_GET['index'] : addslashes($_GET['index']);}

 

$sql = "SELECT * FROM name WHERE LEFT(`name1`,0)='{$colname}' ORDER BY name1 ASC ";

        $result = mysql_query($sql);

         

  while($row = mysql_fetch_assoc($result)){

  echo $row['name1'] . " " . $row['age'] . '<br />';

}

 

    mysql_free_result($result);

    ?>

and my table structure in my database is this

 

+-------+-----+----+

| name1 | age | id |

+-------+-----+----+

| joe | 19 | 1 |

| abc | 34 | 2 |

| max | 89 | 3 |

| bcd | 45 | 4 |

| ben | 35 | 5 |

+-------+-----+----+

<?php
//database connection settings
$dbcon = @mysql_connect('localhost', 'root', 'root');
if (!$dbcon) {
    exit('Error Connecting To The Site Database.');
}
if (!@mysql_select_db('letters', $dbcon)) {
    exit('Error opening the site database.');
}
?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<a href="searchByList.php?index=#">#[/url]
<a href="searchByList.php?index=A">A[/url]
<a href="searchByList.php?index=B">B[/url]
<a href="searchByList.php?index=C">C[/url]
<a href="searchByList.php?index=D">D[/url]
</body>
</html>

 

<?php
include 'db.inc.php';

$colname = "A";

if (isset($_GET['index'])) {$colname = (get_magic_quotes_gpc()) ? $_GET['index'] : addslashes($_GET['index']);}

$sql = "SELECT * FROM name WHERE LEFT(`name1`,1)='{$colname}' ORDER BY name1 ASC ";
        $result = mysql_query($sql);
         
      while($row = mysql_fetch_assoc($result)){
  echo $row['name1'] . " " . $row['age'] ."<br>"; // added a br tag here
}

    mysql_free_result($result);

?>

<?php
$colname = "B";

if (isset($_GET['index'])) {$colname = (get_magic_quotes_gpc()) ? $_GET['index'] : addslashes($_GET['index']);}

$sql = "SELECT * FROM name WHERE LEFT(`name1`,0)='{$colname}' ORDER BY name1 ASC ";
        $result = mysql_query($sql);
         
   while($row = mysql_fetch_assoc($result)){
  echo $row['name1'] . " " . $row['age'] . '<br>';// added a br tag here
}

    mysql_free_result($result);
    ?> 

 

I added the br tags into the code above. Try that and post back

No, this is exactly what its supposed to do.

 

When you are doing a loop and you want each item to be on a separate line you have to add the br tag where you want the new line to be. If you have multiple loops as you do, then a br tag has to be added to each one because adding it to one part will not transfer it to another place in the script.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.