jQuery and CSS not working when generated from PHP - php

I'm trying to create a follow button (like twitter) and it's largely working, however when I generate the HTML from PHP script it's not.
Here's the fiddle to show it working: http://jsfiddle.net/MCam435/HpAWH/10/
Works wonderfully :)
However when I generate it from PHP:
PHP Script
function getTalent($row) {
return "<div class='talent_result_wrapper' data-experience='" . $row['divTagExp'] . "' data-salary='" . $row['divTagSal'] . "'>
<div class='talent_result_header'>
<span class='talent_result_head'>ID: </span>" . $row['CandidateID'] . "
</div>
<ul>
<li><strong>Resides: </strong>" . $row['Town'] . "</li>
<li><strong>Salary Required: </strong>£" . $row['SalaryMin'] . "</li>
<li><strong>Experience: </strong>" . $row['CandidateExperience'] . " Years </li>
<li><strong>Industy: </strong>" . $row['PrimarySector'] . "</li>
<li><strong>Specialism: </strong>" . $row['PrimarySector'] . "</li>
<br>
<div id='follow1'><a href='#' class='follow' id='1'><span class='follow_b'> + Tag </span></a></div>
<div id='remove1' style='display:none'><a href='' class='remove' id='1'><span class='remove_b'> - UnTag </span></a></div>
</div>";
}
Main Page
while($row = mysqli_fetch_array($result2)) {
echo getTalent($row);
}
The switching between div's doesn't work, even though the output is exactly the same?

You can't have multiple elements with the same ID on a page.
<div id='follow1'><a href='#' class='follow' id='1'><span class='follow_b'> + Tag </span></a></div>
<div id='remove1' style='display:none'><a href='' class='remove' id='1'><span class='remove_b'> - UnTag </span></a></div>
You need to give those elements all unique IDs. It will work if there is only 1 instance of this on the page, but as soon as you add more than 1 it will start to fail. That's why it works in your example, but not when you use PHP.

Related

How to load data from MYSQL server with PHP while when loading the website

I have a html file showing a quiz webside with questions and answers and a php file getting the questions from my local mysql server.
The html file and the php file are working fine, but now I want to get the data from the server when the side is loaded and put the data into the right buttons and labels (Loading a question and the answers and display it on my prefabricated html file).
This is my php code how i get the question from my database:
$round = $_POST['round'];
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
$sql = "select * from questions where round = $round order by rand()
limit 1;";
while($row = mysql_fetch_assoc($sql)) {
echo $row['id'] . " " . $row['category'] . " " . $row['round'] .
" " . $row['question'] . " " . $row['rightanswer'] . " " .
$row['wronganser1'] . " " . $row['wronganser2'] . " " .
$row['wronganser3'];
}...
and this is my html file displaying the label and buttons with the question and answers:
<div id="question">
<h4 id="category">Kunst und Literatur</h4>
<p style="margin-bottom: 0px" class="question">Question=</p>
</div>
<ul id="answers">
<li id="answer1" class="button">Answer1</li>
<li id="answer2" class="button">Answer2</li>
<li id="answer3" class="button">Answer3</li>
<li id="answer4" class="button" style="margin-bottom: 0px">Answer4</li>
</ul>...
Instead of "Question" and "Answer1","Answer2"... i want the data loaded from my server.
I appreciate every idea and if anything is unclear don't hesitate to ask.
Thanks :)
If you need to put some content from the database into your webpage you can just replace the HTML code like that:
<div id="question">
<h4 id="category">Kunst und Literatur</h4>
<p style="margin-bottom: 0px" class="question"><?php echo $row['question']; ?></p>
</div>
<ul id="answers">
<li id="answer1" class="button"><?php echo $row['answer1']; ?></li>
<li id="answer2" class="button"><?php echo $row['answer2']; ?></li>
<li id="answer3" class="button"><?php echo $row['question']; ?></li>
<li id="answer4" class="button" style="margin-bottom: 0px">Answer4</li>
</ul>...
If you want to make a site with more questions just wrap your while loop over the HTML code.

Need to loop a block of code using while loop

The content is comming from a query and I dont whant to manually generate a long and repetitive block of code, so I thoght it would work nice if I put the first chunk into a while loop but, nothing good comes out of it.
Here is what I got so far...
<?php
$bloq_1 = array(1,2,3,4,5,7,8,9,10);
$blnu_1 = '1';
while( $bloq_1=$numeral_1) {
echo $numeral_1="<article class=\"notxtras\">
<a class=\"notxtras_url_cntn\" href=\"cdn.php?".$tema_s[$blnu_1++]['pltfrm']."=".$tema_s[$blnu_1++]['notid']."\" title=\"".$tema_s[$blnu_1++]['ttl']."\">
<div class=\"notxtras_img_cntn\">
<img src=\"http://cadenanoticias.mx/img/miniatura/".$tema_s[$blnu_1++]['pic1'] ."\" alt=\"".$tema_s[$blnu_1++]['rlcn'] ."\">
</div>
<h1 class=\"notxtras_ttl_cntn\">".$tema_s[$blnu_1++]['ttl']."</h1>
<p class=\"notxtras_brv_cntn\">".$tema_s[$blnu_1++]['brv'] ."</p>
<p class=\"notxtras_dsp_cntn\">Por: ".$tema_s[$blnu_1++]['aut'] ." • ".$tema_s[$blnu_1++]['cdd']." • ".ucfirst(strftime("%A %e de %B del %Y",date(strtotime($tema_s[$blnu_1++]['fch'])))) ."</p>
</a>
</article>";
}
?>
Is it posible?
I have three suggestions/comments:
If the content comes from a query, it would probably be better to use foreach to iterate over the actual query results (which appear to be stored in $tema_s), rather than iterating over a range of numbers. I don't know exactly how $tema_s is populated, but if it's like most other query results I've seen, you're probably missing item 0 if you use [1,2,3,4,5,7,8,9,10]. If you're doing this in order to only show ten results, it would be much better to add a LIMIT clause to your query so you won't be fetching more data than you need.
In cases where you find yourself echoing lots of HTML, it may be better to exit from PHP to produce the HTML, and just echo values from PHP where you need them. This will prevent the annoyance of escaping all those quotes, and the mess it will create when you miss one (not saying you have in this case, but it's quite easy to do.)
None of your variables have been properly escaped for HTML output.
Adjusting the code according to these ideas would be something like this:
<?php
foreach ($tema_s as $item):
$query_string = urlencode($item['pltfrm']. '=' .$item['notid']);
$title = htmlspecialchars($item['ttl']);
$src = urlencode($item['pic1']);
$alt = htmlspecialchars($item['rlcn']);
$date = ucfirst(strftime("%A %e de %B del %Y", date(strtotime($item['fch']))));
?>
<article class="notxtras">
<a class="notxtras_url_cntn" href="cdn.php?<?= $query_string ?>" title="<?= $title ?>">
<div class="notxtras_img_cntn">
<img src="http://cadenanoticias.mx/img/miniatura/"<?= $src ?>" alt="<?= $alt ?>">
</div>
<h1 class="notxtras_ttl_cntn"><?= $title ?></h1>
<p class="notxtras_brv_cntn"><?= htmlspecialchars($item['brv']) ?></p>
<p class="notxtras_dsp_cntn">
Por: "<?= htmlspecialchars($item['aut']) ?>
• <?= htmlspecialchars($item['cdd']) ?>
• <?= $date ?>
</p>
</a>
</article>";
<?php endforeach; ?>
Also, as mentioned in the comments on your question, you should consider looking into a template system, such as twig. It may seem like overkill for what you're doing here, but it takes care of a lot of this kind of stuff for you.
This should work:
<?php
$bloq_1 = array(1, 2, 3, 4, 5, 7, 8, 9, 10);
foreach ($bloq_1 as $blnu_1) {
echo "<article class=\"notxtras\">
<a class=\"notxtras_url_cntn\" href=\"cdn.php?" . $tema_s[$blnu_1]['pltfrm'] . "=" . $tema_s[$blnu_1]['notid'] . "\" title=\"" . $tema_s[$blnu_1]['ttl'] . "\">
<div class=\"notxtras_img_cntn\">
<img src=\"http://cadenanoticias.mx/img/miniatura/" . $tema_s[$blnu_1]['pic1'] . "\" alt=\"" . $tema_s[$blnu_1]['rlcn'] . "\">
</div>
<h1 class=\"notxtras_ttl_cntn\">" . $tema_s[$blnu_1]['ttl'] . "</h1>
<p class=\"notxtras_brv_cntn\">" . $tema_s[$blnu_1]['brv'] . "</p>
<p class=\"notxtras_dsp_cntn\">Por: " . $tema_s[$blnu_1]['aut'] . " • " . $tema_s[$blnu_1]['cdd'] . " • " . ucfirst(strftime("%A %e de %B del %Y", date(strtotime($tema_s[$blnu_1]['fch'])))) . "</p>
</a>
</article>";
}

How to make a userfeed scroll on a new mysql input?

I'm trying to make a userfeed for the homepage of my website that displays new information from users, for example: user x just did somthing.
This is what it looks like sofar: Image of page
this is the code used on the page:
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo '
<div class="media testimonial-inner">
<div class="pull-left">
<img class="img-responsive img-square" src="images/bierportaal.jpg" height="66" width="66">
</div>
<div class="media-body">
<span><a href="' . $row["naam"] . '"target=main>' . $row["naam"] . '</a></span>
<span>' . $row["tekst_before"] . '</span><a href="' . $row["item"] . '"target=main>' . $row["item"] . '</a>
<span>' . $row["tekst_after"] . '</span>
</div>
</div>
';
}
} else {
}
$db_conx->close();
?>
My question is, How do I rotate trough the activities?
So that when someone does somthing (it will be inserted into the database)the records jump and the top one will display a new record and the bottom one wil move out of the screen/disapear.
An example of what i have in mind is on this website the activetyfeed:
ratebeer website
Kind regards,
Aerosteon

How to change the "while" loop to retrieve only 3 top records from MySQL database

I set up the query to SELECT * FROM,
Then I used the while loop to print out all retrieved records and it all works fine.
But what I need is to print out only the TOP 3 of those records, is there any way to change that while loop to make it work this way.
Any help on this will be much appreciated, thx
while($row = $alerts_query->fetch_object()){
echo "<div class='panel'>
<div class='panel-heading'>
<h3 class='panel-title'>" . $row->ticket_title . " <a href='clientpanel.php?tab=dashboard=" . $row->ticketId . "' aria-hidden='true' type='button' class='close pull-right'>x</a></h3>
</div>
<div class='panel-body'>" . $row->ticket_description . "
<br />
<br />
<span style='color: #999'>Reported: " . $row->ticket_date . "</span>
</div>
</div>";
}
Do not limit in the while loop (though, it's possible). Change your SQL instead:
SELECT * FROM yourtable
Append:
SELECT * FROM yourtable LIMIT 3
If for some reason you should do it in PHP (not advised in this situation):
$i = 0;
while($row = $alerts_query->fetch_object() and $i<3) {
$i++;
// ...
}
Use:
SELECT * FROM [table] LIMIT 3
Use
SELECT * FROM table LIMIT x ORDER BY ASC
where x in your case is 3
It can achieved in many ways, using LIMIT clause is one of the approach. But following is according your scenario
$i = 1;
while ($row = $alerts_query->fetch_object()) {
if ($i > 3)
break;
echo "<div class='panel'>
<div class='panel-heading'>
<h3 class='panel-title'>" . $row->ticket_title . " <a href='clientpanel.php?tab=dashboard=" . $row->ticketId . "' aria-hidden='true' type='button' class='close pull-right'>x</a></h3>
</div>
<div class='panel-body'>" . $row->ticket_description . "
<br />
<br />
<span style='color: #999'>Reported: " . $row->ticket_date . "</span>
</div>
</div>";
$i++;
}
Try this
SELECT * FROM table LIMIT 0,3

Steam Login script won´t work

I tried to use the steam-login script of github on my php file.
I used a custom button instead of the official steam button.
The situation is: I press the button and i get to the standard localhost/dashboard site, I'm simulating it over XAMPP.
Script to login to steam, adding a trade link of your steam account, showing your profile and logging out at the end.
<?php
if(!isset($_SESSION["steamid"])) {
steamlogin();
echo '<a class="ezpz-loginas" style="font-size: 12px;border: 2px solid #db073d;margin: 60px auto 0px;padding-top: 22px;" href="/?login">CONNECT TO STEAM</a>';
} else {
echo '<article class="jfrs clearfix">
<img src="' . $steamprofile['avatarfull'] . '" height="74" width="74" alt="">
<p>' . $steamprofile['personaname'] . ' Logout</p>
<ul class="clearfix">
<li>Where can I find my Trade-URL?</li>
</ul>
</article>
<form method="POST" action="./updatelink.php">
<input type="text" class="main-link" name="link" id="link" value="' . $mytrade . '" placeholder="Set Trade-URL">
<input type="submit" class="main-link-check" href="#" value="">
</form>';
mysql_query("UPDATE users SET name='" . $steamprofile['personaname'] . "', avatar='" . $steamprofile['avatarfull'] . "' WHERE steamid='" . $_SESSION["steamid"] . "'");
}
?>
I edited the steamauth files in the right way.
P.S.: Sorry for my english skills.

Categories