Generated php results appear and then promptly disappear - php

So I am trying to create a tutorial bookmarker project. The idea is that the bookmarked tutorials are supposed to load when the page opens. I am trying to display the results from the mysql database using php, for some reason the results only display for half a second and then disappear.
I have spent ages trying to figure out how to fix it and have had no luck. I think it might have something to do with the while loop but not sure.
I have created a class that contains the simple load bookmarks function.
<?php
class Bookmarked {
private $con;
public function __construct($con) {
$this->con = $con;
}
public function loadBookmarks(){
$str = "";
$data = mysqli_query($this->con, "SELECT * FROM bookmarked ORDER BY id DESC");
$count = mysqli_num_rows($data);
if($count == 0){
echo "No bookmarks saved";
}
while($row = mysqli_fetch_array($data)){
echo "In while loop";
$tutname = $row['tutname'];
$tutdescription = $row['tutdescription'];
$tuturl = $row['tuturl'];
$date_added = $row['date_added'];
$str .= "<div class='well'>
<h3>$tutname</h3> <hr> <br>
<p>$tutdescription</p> <br>
<a class='btn btn-primary' href='#'><i class='fas fa-check'></i></a>
<a class='btn btn-default' href='$tuturl' target='_blank'><i class='fas fa-search'></i></a>
<a class='btn btn-danger' href='#' onclick='deleteBookmark()'><i class='fas fa-trash'></i></a>
</div>";
}
echo "Out of while loop";
echo $str;
}
}
?>
I am trying to call it as shown below
<?php
include("includes/header.php");
include("includes/classes/Bookmarked.php");
?>
<div class="header">
<h3 class="text-muted">Tutorials Bookmarked</h3>
</div>
<div class="row">
<div class="col-lg-12">
<div id="bookmarksResults">
<?php
$bookmark = new Bookmarked($con);
$bookmark->loadBookmarks(); //Calling the loadBookmarks() function
?>
</div>
</div>
</div>
</body>
</html>
I tried search for an answer but couldn't find anything. If this isn't clear enough I will explain further.

Related

add limit to data passed to bootstrap modal with foreach() in codeigniter

I'm not experiencing any difficulty with pulling data from a db table in CodeIgniter and displaying it in a bootstrap modal window. Using this button link;
<a href='#' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a>
I'm able to access the modal window no problem and display the results of this following coding;
<!-- alert view -->
<div class='modal fade' id='myModal' role='dialog'>
<div class='modal-dialog'>
<!-- Modal content-->
<div class='modal-content'>
<div class='modal-header'>
<?php
foreach ($article->result() as $a) {?>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h1 class='modal-title'><?php echo $a->title; ?></h1>
</div>
<div class='modal-body'>
<h4><?php echo $a->category_name; ?></h4>
<h4><?php echo $a->public_date; ?></h4>
<h4><?php echo $a->description; ?></h4>
<?php } ?>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
</div>
</div>
The exact issue I'm having is imposing a limit on the returned results of the foreach() loop. If I use something like $i=0; if($i==1) break; before the foreach(), I get the same content article_id result. To add to my confusion and difficulty, when I try to make article_id the href of the View link, ALL db table entries are pulled into the modal window.
I tried working through the query builder chapter in the CI docs dealing with single result arrays as that was most applicable to my situation. That was the basis for the following approach below;
CONTROLLER
$data['a'] = $this->article_model->get_modal();
$this->load->view('article_submit',compact('article', 'page_link', 'category'), $data);
MODEL
function get_modal()
{
$results = array();
$this->db->select('*');
$this->db->from($this->tbl_article);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$results = $query->result();
}
return $results;
}
VIEW
<?php
foreach ($article->result() as $a) {?>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h1 class='modal-title'><?php echo $a->title; ?></h1>
</div>
<div class='modal-body'>
<h4><?php echo $a->category_name; ?></h4>
<h4><?php echo $a->public_date; ?></h4>
<h4><?php echo $a->description; ?></h4>
<?php } ?>
I ended up keeping the VIEW coding as it was working for me without the model/controller code. I've actually spent an extra hour on research, trying desperately to ascertain there weren't any answers in the wild on Google or SO. I even gave it one more Doug Flutie Hail Mary;
VIEW
<?php
$i=0;
foreach ($article->result() as $a) if($i==1) break; {?>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h1 class='modal-title'><?php echo $a->title; ?></h1>
</div>
<div class='modal-body'>
<h4><?php echo $a->category_name; ?></h4>
<h4><?php echo $a->public_date; ?></h4>
<h4><?php echo $a->description; ?></h4>
<?php $i++; ?>
<?php } ?>
My head is literally pounding # this point...thanks for any clues around this. It could also be something I'm completely missing as well as there's no holiday from Human Error (lol!).
UPDATE
Currently I'm stuck trying to retool using solutions suggested by #manu joseph & #webcrazymaniac - #webcrazymaniac: not sure if I understand your question about how the article list is built. It may be that I haven't posted enough code for you - please inform me as to what you need to see. As far as the inclusion of the echo for the #modalItem of the View link - not doing anything different for me. #manu joseph, I cannot change $article->result() to article->row() as the former is how the article data is displayed to the page (ie);
<?php
foreach ($article->result() as $a)
{
echo "<tr>".
"<td>$a->article_id</td>".
"<td>$a->title</td>".
"<td>$a->category_name</td>".
"<td>$a->access_level</td>".
"<td>$a->public_date</td>".
"<td><img class='img-responsive img-circle' style='width:100%; height:50px' src='".base_url()."images/posts/$a->image' title='Image' ></td>".
"<td>".
"<a href='".$a->article_id."' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a> ".
"<a href='".base_url()."article/view/$a->article_id' class='btn btn-primary btn-sm'>Edit</a> ".
"<a href='".base_url()."article/delete/$a->article_id' class='btn btn-danger btn-sm'>Delete</a> ".
"</td>".
"</tr> ";
}
?>
the above being the table that db results are being echoed out to. #manu joseph, I've noticed initially that the $i=0; lines are only ever functional when I don't try to add the article_id in the View button link. That's where the answer lies for me, since, when that happens, then I'm back to the problem that the modal only displays a single listing based on the click of the View button link, but it happens to grab the identical singular result to display within the modal no matter WHAT View button link is clicked upon. Funny how now my previous pagination problems are non-existant (lol!).
SO CLOSE I CAN TASTE IT...
An hour's worth of tweakling has gotten me to the desired (if skewed) result of just displaying a single articles' details;
MODAL WINDOW VIEW LINK
"<a href='".$a->article_id."' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a> ".
MODAL CONTENT
<div class='modal-content'>
<div class='modal-header'>
<?php
foreach ($article->result() as $a) $i=0; if($i==1) break; {
?>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h1 class='modal-title'><?php echo $a->title; ?></h1>
</div>
<div class='modal-body'>
<h4><?php echo $a->category_name; ?></h4>
<h4><?php echo $a->public_date; ?></h4>
<h4><?php echo $a->description; ?></h4>
<?php $i++; ?>
<?php } ?>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
</div>
PICTURED RESULT:
I don't know that you can see (probably with image enlargement) the cursor arrow hovering over the 1st View button on page, but the specific article_id is not being returned. Like I said, so close I can taste it.
Misery Index UPDATE:
Thank you so much #manu joseph for his continuing attempt to set me right - the code;
<?php
$i = 0;
foreach ($article->result() as $a)
{
echo $i."<br>";
if( $i == 1 ) break;
?>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h1 class='modal-title'><?php echo $a->title; ?></h1>
<div class='modal-body'>
<h4><?php echo $a->category_name; ?></h4>
<h4><?php echo $a->public_date; ?></h4>
<h4><?php echo $a->description; ?></h4>
</div>
<?php
$i++;
}
die();
?>
Results in the image below;
The value of $i is echoed at the top above the title and the incremented value $i++ is echoed at bottom of modal and the close button is vanished. I'm playing a bit with your sequencing as it's neater than what I have currently.
I think what needs to be addressed is the View button links as when they're hovered over, the correct article_id is shown, but the modal always only manages to pull in the 1st recordset and never increments to show the content under any of the other article ids;
"<a href='".$a->article_id."' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a> ".
I know I'm fluffing it right here and am in a fight to connect with the other article ids by missing something crucial. Any other questions you need answered, please feel free to ask. Gotta bed it now...
UPDATE
#webcrazymaniac listed a solution that took me several readings to actually catch his intent to help push me into a better direction and holding position # the moment;
foreach ($article->result() as $a) {
echo "<a href='".base_url('#myModal/$a->art_id')."' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a> ".
<?php } ?>
the above being the link that drops the modal into view. I'm now able to pull the 1st db result into all the modals of the page choices;
<!-- alert view -->
<div class='modal fade' id='myModal' role='dialog'>
<div class='modal-dialog'>
<!-- Modal content-->
<?php
$i = 0;
if (!$i = 0){
foreach (array_slice($article->result(), 0, 1) as $a) {
?>
<div class='modal-content' id='myModal<?php echo $a->art_id;?>'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h4 class='modal-title'><?php echo $a->title;?></h4>
</div>
<div class='modal-body'>
<h2><?php echo $a->description;?></h2>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
</div>
<?php
break;
}
}
?>
</div>
Using array_slice() was a way for me to limit how many results were pulled in from the links as I couldn't specify a limit without breaking the previous (private) definition for limit related to pagination (and I definitely didn't want to disturb that) within the model. My current modal function;
public function modal($art_id=NULL)
{
$this->db->select('*');
$this->db->from('tbl_article');
$this->db->where('tbl_article.art_id =', $art_id);
$result = $this->db->get();
return $result->result_array();
}
With only a hairsbreadth distance from my resolution, I'm a little to brain-squirrely to see the forest from the trees at this point and am staunchly convinced it's the most trivial of resolutions, but just cannot see it.
To summarize, your first issue
The exact issue I'm having is imposing a limit on the returned results
of the foreach() loop. If I use something like $i=0; if($i==1) break;
before the foreach(), I get the same content article_id result.
I believe u need to limit the number of rows displayed in view? if yes, you need to do the checking inside foreach loop (U have put this before the braces starts :) ) as below:-
<?php
$i=0;
foreach ($article->result() as $a) {
if($i==1) break;
?>
<button type='button' class='close' data-dismiss='modal'>×</button>
Moreover if you want to get only the first row, go for
foreach ($article->row() as $a) {
Edited Lines Below >>>>>>>>>
Hi, #HomeOffice I have modified the view code as below :-
<?php
$i = 0;
foreach ($article->result() as $a) {
echo $i."<br>";
if( $i == 1 ) break;
?>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h1 class='modal-title'><?php echo $a->title; ?></h1>
<div class='modal-body'>
<h4><?php echo $a->category_name; ?></h4>
<h4><?php echo $a->public_date; ?></h4>
<h4><?php echo $a->description; ?></h4>
</div>
<?php
$i++;
}
die();
?>
Print the value of "i".
Removed some unnecessary div.
Put a die() at the end.
Let me know what is your output? ..
Also let me have you tried this with $article->row() ?
You should use an id for each element in your table. Then call that id in your button:
<a href='#modalItem<?php echo $a->id;?>' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a>
That should do it, although my answer could be more complete, but I get the feeling you haven't posted all you should have posted. More precise, I can't get it how you build the list of articles behind the modal window.
UPDATE:
If I understand this right, this is the button that toggles the modal element:
<a href='".$a->article_id."' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a>
If so, you must add the 'id'(#) simbol in the href:
<a href='#articleModal".$a->article_id."' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a>
(not sure what triggers your modal window, if it's data-target='#myModal', it's here where you must make the modification: #myModal".$a->article_id.")
This tells the browser you have an unique id for each modal window, that corresponds to each article. And the result would be a link to #articleModal[x], on the same page.
Only at this point, you build only one modal, (with several articles inside when you started, and one article now). That's because you use foreach iteration inside the modal element, instead of using it to build a different modal window, with a different id, for each article.
What you need to do is something like this:
<?php
foreach ($article->result() as $a){
?>
<div class='modal-content' id='articleModal<?php echo $a->article_id;?>'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h1 class='modal-title'><?php echo $a->title; ?></h1>
</div>
<div class='modal-body'>
<h4><?php echo $a->category_name; ?></h4>
<h4><?php echo $a->public_date; ?></h4>
<h4><?php echo $a->description; ?></h4>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
</div>
<?php } ?>
, or something like this.
The idea is to have a unique modal window, with it's unique id for each article, uniquely called with the corresponding button.
In all actuality - according to the phrasing of my question - I've answered it already;
<?php
foreach (array_slice($article->result(), 0, 1) as $a) {
?>
using array_slice I was able to achieve the limitation of results being pulled into the modal. The problem of not being able to return the matching article content by id is outside the scope of the original question and requires its' own separate focus. Thanks to #manu joseph and #webcrazymaniac, respectively, for their efforts on this issue.

Some HTML tags disappear when i use PHP

I'm trying to generate dynamic contents fill from mysql db
here is my php code:
<?php
include 'header.php';
error_reporting(1);
$user = "root";
$pass = "";
$dbName = "haimi";
mysql_connect('localhost', $user, $pass) or
die("Could not connect: " . mysql_error());
mysql_select_db($dbName);
$sql = "SELECT * FROM Projects ";
$result = mysql_query($sql);
?>
<?php
while ($line = mysql_fetch_array($result)) {
?>
<li class="filter" data-filter=".cat<?php echo $line['Category'];?>"><?php echo $line['Category'];?></li>
<?php
}
?>
The li displays correctly, but the following does not:
<div class="row projects m0">
<?php
while ($line = mysql_fetch_array($result)) { ?>
<div class="project mix catHouses">
<div class="tint"></div>
<a href="images/projects/".<?php echo $line['ProjectImage1']; ?> data-
lightbox="project" data-title="Central Hospital (building)">
<img src="images/projects/".<?php echo
$line['ProjectImage1']; ?> alt="<?php echo $line['ProjectTitle'];?>"
class="projectImg"> </a>
<div class="projectDetails row m0">
<div class="fleft nameType">
<div class="row m0 projectName"><?php echo $line['ProjectTitle'];?></div>
<div class="row m0 projectType"><?php echo $line['ProjectType'];?></div>
</div>
<div class="fright projectIcons btn-group" role="group">
<a href="images/projects/<?php echo $line['ProjectImage1']; ?>" data-lightbox="project" data-title="Central Hospital (building)" class="btn btn-default">
<i class="fa fa-link"></i></a>
<i class="fa fa- search"></i>
</div>
</div>
</div>
<?php
}
?>
</div>
It data in the divs doesn't appear.
You're making a single call, but trying to loop through it twice. To do so, you need to reset the pointer back to the beginning:
//Add this after the first loop, but before the second
mysql_data_seek( $result, 0 );
The way you have it now, it's while($line = mysql_fetch_array($result)), but the second loop is never entered since it has already reached the end. Since the loop is ended, it never displays the contents.
Important Note
The mysql_* functions are deprecated, and is removed in PHP 5.5. You should use Mysqli or PDO. They have better protections against mysql injections (see Bobby Tables).
You have some HTML mistakes here:
<a href="images/projects/".<?php echo $line['ProjectImage1']; ?>
First, there is no need to use . operator (as you are in HTML, not PHP),
Also you shoud put your <?php ?> tag inside the href quotations, here is the correct code:
<a
href="images/projects/<?php echo $line['ProjectImage1']; ?>"
data-lightbox="project"
data-title="Central Hospital (building)"
>
<img
src="images/projects/<?php echo $line['ProjectImage1']; ?>"
alt="<?php echo $line['ProjectTitle']; ?>"
class="projectImg"
>
</a>
You will get older fast writing code like that ;)
How about this:
while ($line = mysql_fetch_array($result)) {
$category = $line['Category'];
echo <<< LOB
<li class="filter" data-filter="$category">$category</li>
LOB;
}
Like what Mr #matthew said
I was making a single call, and I was trying to loop through it twice.
The problem solved with this code before the while loop:
$result = mysql_query($sql);

best practices for echo statement that outputs html?

Hi guys I have an application that allows users to post content on an empty div. I need some advice on making it scalable as I have to make sure if users are posting a lot of content the browser will not slow down or crash. I am a beginner so please put up with me. So in my code below I have an echo statement that prints out html content. I want to know how to make that function scalable in the long run? should I set the html as a variable then print it? should I encode it with JSON? please advice me as such. Thank-you
PHP Code
<?php
include_once("connection.php");
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//You can use Inner Join to display the matched records in between the two tables
$query = "SELECT * FROM `qpost` A INNER JOIN `user` B ON A.id=B.id ";
$result = $conn->query($query);
if($result) {
} else {
echo "bitch: " . $conn->error;
}
while($row = $result->fetch_assoc()) {
$time = time();
if( $time < $row['logged_time'] + 30) {
echo " <div class='row'>
<div class='col-md-8'>
<div id='thePost' class='panel panel-default'>
<div class='panel-heading'>
<h3 class='panel-title'><span class='glyphicon glyphicon-user'> <b>{$row['username']}</b></span> &#8195&#8195 &#8195&#8195 <span class='glyphicon glyphicon-time'></span> Posted_on: {$row['time']} </h3>
</div>
<div class='panel-body' style='word-break:break-all'>
<h4>{$row['question']} </h4>
<p> {$row['description']}</p>
</div>
<div class='panel-footer'>
<button class='btn btn-primary'>Request Connection</button>
<button class='btn btn-success'>Chat <span class='glyphicon glyphicon-comment'></button>
</div>
</div>
</div>
</div> ";
}//end if
}//end while
?>
Did you try to separate the data logic and data display ? if you used php MVC framework which will more convenient on your case.
Following is a simple example.
//demo.html code
<?php
include_once("connection.php");
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//You can use Inner Join to display the matched records in between the two tables
$query = "SELECT * FROM `qpost` A INNER JOIN `user` B ON A.id=B.id ";
$result = $conn->query($query);
if ($result) {
} else {
echo "bitch: " . $conn->error;
}
$time = time();
$data_result = array(); // focus on your data logic process
while ($row = $result->fetch_assoc()) {
if ($time < $row['logged_time'] + 30) {
$data_result[] = $row;
}
}
?>
<?php foreach($data_result as $index => $row) { ?>
<div class='row'>
<div class='col-md-8'>
<div id='thePost' class='panel panel-default'>
<div class='panel-heading'>
<h3 class='panel-title'><span class='glyphicon glyphicon-user'> <b><?php echo $row['username']?></b></span>
&#8195&#8195 &#8195&#8195 <span class='glyphicon glyphicon-time'></span> Posted_on: <?php echo $row['time']?>
</h3>
</div>
<div class='panel-body' style='word-break:break-all'>
<h4><?php echo $row['question']?></h4>
<p> <?php echo $row['description']?></p>
</div>
<div class='panel-footer'>
<button class='btn btn-primary'>Request Connection</button>
<button class='btn btn-success'>Chat <span class='glyphicon glyphicon-comment'></button>
</div>
</div>
</div>
</div>
<?php } ?>
It's fine what you're doing.
However as Eric has said you can exit and re-enter your PHP
You could store the html in a file, grab it using file_get_contents then use sprintf to manipulate it
In a file called "row.html"
<div class='row'>
<div class='col-md-8'>
<div id='thePost' class='panel panel-default'>
<div class='panel-heading'>
<h3 class='panel-title'>
<span class='glyphicon glyphicon-user'>
<b>%s</b> <!-- username -->
</span> &#8195&#8195 &#8195&#8195
<span class='glyphicon glyphicon-time'>
Posted_on: %s <!-- time -->
</span>
</h3>
</div>
<div class='panel-body' style='word-break:break-all'>
<h4>%s</h4> <!-- question -->
<p> %s</p> <!-- description -->
</div>
<div class='panel-footer'>
<button class='btn btn-primary'>Request Connection</button>
<button class='btn btn-success'>Chat <span class='glyphicon glyphicon-comment'></button>
</div>
</div>
</div>
</div>
Then in your PHP....
<?php
while($row = $result->fetch_assoc()) {
$time = time();
if( $time < $row['logged_time'] + 30) {
$rowHtml = file_get_contents('row.html');
echo sprintf($rowHtml,
$row['username'],
$row['time'],
$row['question'],
$row['description']);
}
}
?>
I personally like as much abstraction as possible between my languages :)
Good luck!
One way to echo out HTML with PHP is using Heredoc syntax (http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)
<?php
$name = 'MyName';
echo <<<MYHTML
<p>
My name is <b>"$name"</b>.
</p>
MYHTML;
?>

Nivo slider + php

I created a custom cms for a website and trying to make the nivo slider work with my db but im having issues with my while loop.
im only storing the name of the image in the db and the image itself its in a folder, images are working somewhat but they appear on above the other the the actual slideshow is broken.
my guess is that the title id is breaking it but not sure on how to go from here. any help is appreciated
here is my code:
<div id='slider' class='nivoSlider'>
<?php
$sql = 'SELECT * FROM slider';
$result = $db->query($sql) or die(mysqli_error());
while($row = $result->fetch_assoc()){
$slideshow = $row['slider_id'];
print"
<img src='images/slider/".$row['image'].".jpg' alt='' title='#htmlcaption'>
</div>
<div id='htmlcaption' class='nivo-html-caption '>
<span>".$row['title'] . "</span>
</div> ";
}
?>
<div id='preloader'></div>
</div>
while($row = $result->fetch_assoc()){
$slideshow = $row['slider_id'];
print"
<img src='images/slider/".$row['image'].".jpg' alt='' title'#htmlcaption'>
</div> // ---------------> Here you are closing div slider
<div id='htmlcaption' class='nivo-html-caption '>// ----> Error
<span>".$row['title'] . "</span>
</div> ";
}
In while loop you are closing </div> without opening it,This cause broken slide show.In HTML syntax id's must be unique. So <div id='htmlcaption' class='nivo-html-caption '> so change this part.
[Update]
change print to
print" <div class='some_wraper'>
<img src='images/slider/".$row['image'].".jpg' alt='' title='#htmlcaption'>
</div> // ---------------> Here now you are closing div some_wraper
<div class='nivo-html-caption htmlcaption'>// ----> added new class htmlcaption
<span>".$row['title'] . "</span>
</div> ";
Update
Fixed code
<div id='slider' class='nivoSlider'>
<?php
$sql = 'SELECT * FROM slider';
$result = $db->query($sql) or die(mysqli_error());
for($i = 0;$row = $result->fetch_assoc();$i++){
$slideshow = $row['slider_id'];
echo "<img src='images/slider/".$row['image'].".jpg' alt='' title='htmlcaption_$i'>";
$tiles[$i]=$row['title'];
}
?>
</div>
<?php //caption divs for slider
for($i=0;$i<count($tiles);$i++) {
echo "<div id='htmlcaption_$i' class='nivo-html-caption '>";
echo "<span>".$tiles[$i]."</span> </div>";
}
?>
<div id='preloader'></div>
</div>

passing string to javascript function then show onClick

I looked over some solutions, but I coudn't figure out how to get this solved
here is my php
while($row = mysql_fetch_array($result))
{
echo"<div class='span4'>
<h2>$row[title]</h2>
<p>$row[intro]</p>
<p><a class='btn' onclick=parashow($row['contents'])>View details »</a></p>
</div>";
}
I just want to pass the contents into (a function)
and then this function display on click
on div specific id
looking on the web for several hours couldn't help
some say
add
{}
other /' '/
or
'''$row[contents]'''
stil no help
function parashow(x){
//document.getElementById('allrows').style.display = "none";
}
I don't know how to pass $row['contents']
to function argument
and then
get x
and replace it into another div by id ('something')
Try this
while($row = mysql_fetch_array($result))
{
echo"<div class='span4'>
<h2>$row[title]</h2>
<p>$row[intro]</p>
<p><a class='btn' onclick=parashow('".$row['contents']."')>View details »</a></p>
</div>";
}
It's good to generate HTML without PHP:
<?php while($row = mysql_fetch_array($result)) { ?>
<div class='span4'>
<h2><?php echo $row[title]; ?></h2>
<p><?php echo $row[intro]; ?></p>
<p><a class='btn' onclick="parashow('<?php echo $row['contents']; ?>')">View details »</a></p>
</div>
<?php } ?>
try this
<?php while($row = mysql_fetch_array($result)) : ?>
<div class='span4'>
<h2><?php echo $row[title]; ?></h2>
<p><?php echo $row[intro]; ?></p>
<p>View details »</p>
</div>
<?php endwhile; ?>

Categories