Unable to add div to search result display

<html>
<head>
<link rel="stylesheet" type="text/css" href="design/z.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<title> search eng-search</title>
<body>
     <h2>search eng</h2>
	      <form action='./search.php' method='get'>
          <input type='text' name='k' size='50' value=<?php $_GET['k']; ?> >
          <input type='submit' value='Search'>
		  </form>
		 <?php
           $k = $_GET['k'];
		   $terms = explode(" ", $k);
		   $query = "SELECT * FROM search WHERE ";
		     foreach ($terms as $each){
			    $i++;
			if ($i == 1)
				$query .= "keywords LIKE '%$each%' ";
			else
				$query .= "OR keywords LIKE '%$each%' ";
		}
		mysql_connect ("localhost", "root", "");
	    mysql_select_db("tut");
	    $query = mysql_query($query);
	    $numrows = mysql_num_rows($query);
	if ($numrows > 0){
		while ($row = mysql_fetch_assoc($query)){
					$id = $row['id'];
					$title = $row['title'];
					$description = $row['description'];
					$keywords = $row['keywords'];
					$link = $row['link'];
			
			
					echo '<div id="container"> 
				<div class="accordion">
				<label for="tm" class="accordionitem">
				<h2> $title <span class="arrow">&raquo;</span></h2></label>
				<input type="checkbox" id="tm"/>
				<p class="hiddentext">$description</p>
				</div>';


		/*echo "<h2> $keywords <br><br> $title</a></h2> $description <br /><br />";*/
		}
	}
		else
	
	echo "No results found for \"<b>$k</b>\"";
	mysql_close();
	
	
	?>
	  value of <?php echo $_GET["title"]; ?>
</body>
</html>

this is css for this code

*{
  margin: 0; border: 0;
}
h1{
  text-align: center;
  margin-top: 10%
}
p{
  color: #666;
}
#container{
  width: 400px;
  height: auto;
  margin: 0 auto;
  margin-top: 3%;
  border-top: 1px solid #bdbdbd;
  border-left: 1px solid #bdbdbd;
  border-right: 1px solid #bdbdbd;
}
.accordion label{
  display:block;
  background-color: #eeeeee;
  padding: 10px;
  color: #424242;
  text-shadow: 0 0 2px rgba(255,255,255,0.6);
  cursor: pointer;
  border-bottom: 1px solid #bdbdbd;
  border-top: 1px solid #ffffff;
}

.accordion p{
  color: #424242;
  padding: 10px;
  font-size: 0.8em;
  line-height: 1.7em;
  border-bottom: 1px solid #bdbdbd;
  visibility: hidden;
  opacity: 0;
  display: none;
  text-align: left;
  background-color: #fff;
  margin-top: 0px;
}

#tm:checked ~ .hiddentext{
  display: block;
  visibility: visible;
  opacity: 1;
}

input#tm{
  display: none;
  position: relative;
}

#tn:checked ~ .hiddentext{
  display: block;
  visibility: visible;
  opacity: 1;
}

input#tn{
  display: none;
  position: relative;
}

#to:checked ~ .hiddentext{
  display: block;
  visibility: visible;
  opacity: 1;
}

input#to{
  display: none;
  position: relative;
}

Can you elaborate on your problem? Is the div that you reference in your title, the one found at

					echo '<div id="container"> 
				<div class="accordion">
				<label for="tm" class="accordionitem">
				<h2> $title <span class="arrow">&raquo;</span></h2></label>
				<input type="checkbox" id="tm"/>
				<p class="hiddentext">$description</p>
				</div>';

in your code?

Thank you for reply!

i have written a code for search engine and it works absolutely fine.
now i want to display a search result using div below

<div id="container"> 
				<div class="accordion">
				<label for="tm" class="accordionitem">
				<h2> $title <span class="arrow">&raquo;</span></h2></label>
				<input type="checkbox" id="tm"/>
				<p class="hiddentext">$description</p>
				</div>'

trying to add click to show/hide effect for each search result. but the problem is div works only for first result and also can not display the content of the variable … just display variable name.

this div independently works fine but can not add it to my search.php :worried:

can you please help :blush:

For the variables it is because you used single quotes, so either change them to double quotes or use concatenation.

Can you give me details on “div works only for first result”? As at s glance that div should be written for each result…

I tried double quotes …gives me an error “Parse error: syntax error, unexpected ‘container’ (T_STRING), expecting ‘,’ or ‘;’ in C:\xampp\htdocs\new\search.php on line 107”

“div works only for first result” means for example if i search something and it gives me 4 results, this div shows 4 times. when I click on first div it woks(drop down to show result) but if I try click on second or third instance only first div will opens up no matter where I click.

Be sure to escape the other double quotes, like below.

echo "<div id=\"container\"> 
				<div class=\"accordion\">
				<label for=\"tm\" class=\"accordionitem\">
				<h2> $title <span class=\"arrow\">&raquo;</span></h2></label>
				<input type=\"checkbox\" id=\"tm\"/>
				<p class=\"hiddentext\">$description</p>
				</div>";

Ah that’s because you are using IDs in your HTML in the div for “accordion” and “tm”. IDs are spared to be unique. By outputting it multiple times you are breaking that rule.

that code worked !! Thanks.

any idea how to fix IDs issue? to make it unique or something… As of now I can not think of any alternative… can you please help ?

trust me it will be my last question… if you can help me finding solution for this :grimacing:

If you need to stick with IDs to make the CSS work properly, all I could think of would be to add a counter into the loop you’re using to display the results, and append that counter onto the end of the div ID. And modify the CSS to cover the fact that you’ll have “container1”, “container2” and so on.

Also you need to think about your database access - the old-style mysql_ functions library has been removed from the latest version of PHP, you should look at mysqli or PDO when you get chance.

It would probably be wiser to post that question in the HTML/CSS area or the JS area. As I feel like there should be more than just the simple CSS you have, like JavaScript logic or something that I’m not seeing.

But I definitely know those in the CSS/HTML side will be able to assist you in converting your current markup to work better. Be sure to post the CSS/HTML and a screenshot of the issue (like you did here).

Thank you for your reply!!

It worked !! m planning to submit it at my office and we have more than 1000 articles and if i do that my CSS will be of more thane 9000 lines :smiley: bcos we can not add loop in css
i will definitely need to find an alternative to that…

do u know were ill find readymade js or css…

thanks again!

sure ill do that… thanks !!!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.