Jump to content

selected query result placing in more DIVs


thara

Recommended Posts

Im trying to select 3 testimonials at a stretch from the database.. It is ok for me. But my problem is I need place this 3 testimonials in 3 DIVs in the same time.. can anybody tell me how can I do this?

 

   $q = "SELECT * FROM testimonials ORDER BY RAND() LIMIT 1";
   $r = mysqli_query($dbc, $q);

  while ( $row = mysqli_fetch_array($r, MYSQL_ASSOC) ) {


  }

echo '<div class="testimonial-1">
  		   <p>'. $testimonial. '</p>
	  </div>';

echo '<div class="testimonial-2">
  		   <p>'. $testimonial. '</p>
	  </div>';

echo '<div class="testimonial-3">
  		   <p>'. $testimonial. '</p>
	  </div>';

 

Thank you..

Link to comment
Share on other sites

With an array.

 

Don't waste our time. If you have code and you need actual help with a specific part of it, post it and explain what doesn't do what you want it to do, and what you've tried. This "what if I want to do A but then really I mean B and I won't bother to post my real super secret code" stuff is annoying. 

Link to comment
Share on other sites


//List out specific fields instead of using '*'
$query = "SELECT testimonial, name, address, image, url
	 FROM testimonials
	 INNER JOIN table1 ON testimonials.something = table1.something
	 INNER JOIN table2 ON testimonials.something = table2.something
	 ORDER BY RAND() LIMIT 3";
$result = mysqli_query($dbc, $query);

$output = '';
$count = 0;
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
$count++;
//Add additional data from query as needed
$output .= "<div class=\"testimonial-{$count}\">";
$output .= "<p>{$row['testimonial']}</p>";
$output .= "</div>";
}

echo $output;

Edited by Psycho
Link to comment
Share on other sites

Psycho your solution is ok.. but there are other DIVs and HTML elements with different class names and IDs in my main testimonialss DIV.

 

This is my original markup..

 

 <div class="container">
<input type="radio" name="nav" id="first" checked/>
<input type="radio" name="nav" id="second" />
<input type="radio" name="nav" id="third" />

<label for="first" class="first"></label>
<label for="second" class="second"></label>
<label for="third" class="third"></label>

<div class="one slide">
<blockquote>
<span class="leftq quotes">“</span> He promptly completed the task at hand and communicates really well till the project reaches the finishing line. I was pleased with his creative design and will definitely be hiring him again. <span class="rightq quotes">„ </span>
</blockquote>
<img src="vQX7jld6.jpg" width="90" height="90" />
<h2>Steve Kruger</h2>
<h6>UI/UX Designer at Woof Design Studio</h6>
</div>

<div class="two slide">
<blockquote>
<span class="leftq quotes">“</span> He promptly completed the task at hand and communicates really well till the project reaches the finishing line. I recommend him to anyone who wants their work done professionally. The project ... <a href="#"> read more</a><span class="rightq quotes">„ </span>
</blockquote>
<img src="47.jpg" width="90" height="90" />
<h2>John Doe</h2>
<h6>Developer Relations at Woof Studios</h6>
</div>

<div class="three slide">
<blockquote>
<span class="quotes leftq"> “</span> He promptly completed the task at hand and communicates really well till the project reaches the finishing line. I was pleased with his creative design and will definitely be hiring him again. <span class="rightq quotes">„ </span>
</blockquote>
<img src="39.jpg" width="90" height="90" />
<h2>Steve Stevenson</h2>
<h6>CEO Woof Web Design Studios</h6>
</div>
</div>​ 

 

I want to add these 3 testimonials neatly to DIVs' .one, .two, .three

Edited by thara
Link to comment
Share on other sites

Then store the 'formatted' divs in variables (like an array as suggested earlier) then output the variables wherever you want them. YOU failed to provide enough details about what you were doing, YOU failed to provide any "real" code to allow us to really understand what you are working with and what you are trying to achieve, and YOU changed the requirements. So, basically, you just failed - which is why the other responder got fed up with you. If you take the time to adequately explain your problem people are happy to help. When you fail to do so you will find that those willing to respond will be few and far between.

 

This is an extremely simple problem:

<?php

/List out specific fields instead of using '*'
$query = "SELECT testimonial, name, title, image, url
			 FROM testimonials
			 INNER JOIN table1 ON testimonials.something = table1.something
			 INNER JOIN table2 ON testimonials.something = table2.something
			 ORDER BY RAND() LIMIT 3";
$result = mysqli_query($dbc, $query);

//Create an array to store the testimonial output
$testimonials = array();
//Format each testimonial and store as new element in array
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
   //Create new testimonial output
   $output  = "<blockquote>\n";
   $output .= "<span class=\"quotes leftq\"> “</span>{$row['testimonial']}<span class=\"rightq quotes\">„ </span>";
   $output .= "<\blockquote>\n";
   $output .= "<img src=\"{$row['image']}\" width=\"90\" height=\"90\" />\n";
   $output .= "<h2>{$row['name']}</h2>\n";
   $output .= "<h6>{$row['title']}</h6>\n";
   //Add output to array
   $testimonials[] = $output;
}

?>

<div class="container">
   <input type="radio" name="nav" id="first" checked/>
   <input type="radio" name="nav" id="second" />
   <input type="radio" name="nav" id="third" />

   <label for="first" class="first"></label>
   <label for="second" class="second"></label>
   <label for="third" class="third"></label>

   <!-- HTML COde for the testimonial ouput -->
   <div class="one slide">
   <?php echo $testimonials[0]; ?>
   </div>

   <div class="two slide">
   <?php echo $testimonials[1]; ?>
   </div>

   <div class="three slide">
   <?php echo $testimonials[2]; ?>
   </div>

</div>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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