thara Posted January 3, 2013 Share Posted January 3, 2013 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.. Quote Link to comment https://forums.phpfreaks.com/topic/272632-selected-query-result-placing-in-more-divs/ Share on other sites More sharing options...
Jessica Posted January 3, 2013 Share Posted January 3, 2013 The problem is....? Quote Link to comment https://forums.phpfreaks.com/topic/272632-selected-query-result-placing-in-more-divs/#findComment-1402898 Share on other sites More sharing options...
thara Posted January 3, 2013 Author Share Posted January 3, 2013 The problem is how I assign 3 testimonials values into 3 different variables in while loop to use them in 3 different places. Quote Link to comment https://forums.phpfreaks.com/topic/272632-selected-query-result-placing-in-more-divs/#findComment-1402904 Share on other sites More sharing options...
Jessica Posted January 3, 2013 Share Posted January 3, 2013 (edited) Use an array... And don't LIMIT 1. Edited January 3, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/272632-selected-query-result-placing-in-more-divs/#findComment-1402919 Share on other sites More sharing options...
thara Posted January 3, 2013 Author Share Posted January 3, 2013 what you mean?? like this? while ( $row = mysqli_fetch_array($r, MYSQL_ASSOC) ) { $testimonials[] = $row['testimonials']; } then $testimonials[0], $testimonials[1], $testimonials[2] etc... Quote Link to comment https://forums.phpfreaks.com/topic/272632-selected-query-result-placing-in-more-divs/#findComment-1402924 Share on other sites More sharing options...
Jessica Posted January 3, 2013 Share Posted January 3, 2013 Well, did it work? Quote Link to comment https://forums.phpfreaks.com/topic/272632-selected-query-result-placing-in-more-divs/#findComment-1402925 Share on other sites More sharing options...
thara Posted January 3, 2013 Author Share Posted January 3, 2013 its works. but in my real query I need to select five values from 3 tables. (testimonials, name, address, image, url). These five values belong to the one testimonial and so on... Then how can I do? Quote Link to comment https://forums.phpfreaks.com/topic/272632-selected-query-result-placing-in-more-divs/#findComment-1402926 Share on other sites More sharing options...
Jessica Posted January 3, 2013 Share Posted January 3, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/272632-selected-query-result-placing-in-more-divs/#findComment-1402929 Share on other sites More sharing options...
Psycho Posted January 3, 2013 Share Posted January 3, 2013 (edited) //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 January 3, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/272632-selected-query-result-placing-in-more-divs/#findComment-1402940 Share on other sites More sharing options...
thara Posted January 3, 2013 Author Share Posted January 3, 2013 (edited) 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 January 3, 2013 by thara Quote Link to comment https://forums.phpfreaks.com/topic/272632-selected-query-result-placing-in-more-divs/#findComment-1402944 Share on other sites More sharing options...
Psycho Posted January 3, 2013 Share Posted January 3, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/272632-selected-query-result-placing-in-more-divs/#findComment-1403056 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.