How would I create a news system? - javascript

I have a database where I can create a news article through PHPMyAdmin with the fields: "Name of the article", "Content", "Writer".
I have already made a "latest news" system, which looks like this:
$result = mysql_query("SELECT * FROM news LIMIT 5");
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
echo $row['Namn'];
echo " av ";
echo $row['Skribent'];
echo "<br />";
}
}
mysql_close($con);
?>
And that is the latest news system (skribent means = writer), (namn means = name (of the article), (av means = by),.
So, now I have two problems. Once these news are displayed, I want them to be clickable and once you click it, you get to the original page where the article is located, how do I do it?
And, how do I do so that when I insert a query it will automatically create a nice page for every article, like most famous sites have. When they create a news it comes like http://google.com/news/how-to-be-a-dork/

Firstly to link articles to a different page you could use the element's onclick method, and use javascript to redirect the user, or you could add <a> tag around the article element.
Secondly, if you would like pretty urls you will have to read up on url re-writing, I unfortunately don't know much about it.

Related

Change user image based on who's logged in

I'm currently working on a login portal.
I already set the log in and log out functions and placed a session start button on every page.
My problem now is that I would like to update the user image based on the identity of who is logged in.
I'm using a template my boss chose, and this is the code where the image is:
<span>
<img src="../../global/portraits/5.jpg" alt="...">
</span>
Just a simple <img> tag with the link where the image is.
My project is in an html file with php implemented, and I tried to do this in the <span> tag, instead of <img>:
<?php
$W = ["Raffaella", "Milena", "Domiziana"];
$M = ["Umberto", "Domenico"];
if ($_SESSION["nome"] == $W) {
echo '<img src="W.jpg">';
} else if ($_SESSION["nome"] == $M){
echo '<img src="M.jpg">';
}
?>
where $_SESSION["nome"] represents the current user logged in. However, it doesn't do anything, not even producing an error.
Can someone explain how I can do it correctly, please? I'm new to php and trying to understand by studying alone on the internet
I was also considering JavaScript for this work (instead of PHP), but I don't know where to start
It looks like both $W and $M are array of names, rather than a single user, if that's the case, I assume you're trying to determine if a user is in that array. In PHP, you can use:
in_array($_SESSION["nome"], $W)
https://www.php.net/manual/en/function.in-array.php
which will return true/false.
if (true === in_array($_SESSION["nome"], $W)) {
echo '<img src="W.jpg">';
} else if (true === in_array($_SESSION["nome"], $M){
echo '<img src="M.jpg">';
}
But, this only gives two separate images for up to 5 unique names, and I'm not sure if that's what you're going for.

SimpleHtmlDom take elements from pagination built with javascript

So i already have used SimpleHtmlDom with success. The difference is that i iterated through the pagination based on the url.
For example the domain would be:
www.example.com/articles
and the pagination would be
www.example.com/articles/page/1.html
Now i have a pagination that is based on javascript and i get only the results from the first page. Now, when i tell the page to get to the next page, i assume that it reloads the page, so it goes from the start again because the url does not change.
One extra problem is that the link that belongs to the "next page", if you copy it and paste it on the URL bar then it redirects you to another section of the website. (So much for developers -_- )
I will actually paste the original code so you can see by yourself what is going on with the links.
Anyways my question is the following:
How can i iterate through the whole pagination?
<?php
require "simple_html_dom.php";
$url = "http://es.4story.gameforge.com/ranking/browse?type=world";
$baseUrl = "http://es.4story.gameforge.com/ranking/browse?type=world";
//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a URL
$html->load_file($url);
// Get all links
foreach ($html->find('.enc_controls .enc_pager a.enc_next') as $anchor) {
$tempUrl = $baseUrl . $anchor->href;
$linkHtml = file_get_html($tempUrl);
foreach ($linkHtml->find('#results td .item_classWrap a span') as $item ) {
echo $item;
echo "<hr/>";
}
}

Using Javascript to change style of elements in PHP

I have a problem with javascript in PHP. I want to use Javascript function to change ccs atribute display to block for only those div elements that contain certain text that is in php variable which is taken from column in MySQL table. I don't know if it is actually possible this way but here's the code:
PHP:
$sql="SELECT * FROM Filter1";
$result=$conn->query($sql);
while($row = $result->fetch_assoc()) {
$meno=$row['meno'];
echo '<script type="text/javascript"> filterTovar() </script>';
JavaScript:
function filterTovar()
{
var meno='<?php echo $meno; ?>';
var produkt=document.getElementsByClassName("tovar");
produkt.style.display="none";
for(var i=0;i<produkt.length;i++)
{
produkt[i].style.display="none";
}
for(var i=0;i<produkt.length;i++)
{
if(produkt[i].innerHTML==meno)
{
produkt[i].style.display="block";
}
}
}
I am trying to make some kind of filter that makes only certain divs shows depending on result of table but the code above doesn't work and I'm not sure if it is possible to make work this way.
MySQL table I'm taking data from:
id meno znacka cena op format
425 H81M-S2V GIGABYTE 46.03 DDR3 mATX
426 H110N GIGABYTE 83.05 DDR4 mITX
EDIT: Better explanation of my problem: User uses checkboxes to filter products on my website, in database php creates table with corresponding products. My code above is supposed to take that table and use column "meno" in which is name of product to show only those div that contain string "meno".
EDIT2: I want to ask if it is possible to change or work with elements css via DOMDocument(). If yes it would answer my question.
Yes, it is possible but there is no need to use javascript/jQuery - just use HTML and CSS.
Basically, you have your database result set and you loop through it with a while loop, right?
I guess you are constructing HTML in that while loop.
In each iteration of the loop, test your MySQL column value and add a class to the desired HTML element.
Here is an example:
$out = '<table><tbody>';
while($row = $result->fetch_assoc()) {
$theclass = ($row['something']=='hideme') ? ' hiddenTD' : '';
$out .= '<tr><td class="' .$theclass. '">' .$row['something']. '</td></tr>'
}
$out = '</tbody></table>';
echo $out;
Of course, you would have CSS defined in the document, like:
<style>
.hiddenTD{display:none;}
</style>
This is a terrible example because you can't "hide" a TD, but it's an example.

how to scroll an array of text?

I am currently working on a web site for a boy scout troop and I have a database of young men that have earned there eagle scout. I was wanting that data to scroll across a text widget in WordPress. I currently have the plugins to make it work I'm just struggling with the code side of it. I have some php code to send the database to my array. My current issue is getting a dynamic array from my php to my js function and then making it scroll from right to left. what would be the best way to go about this?
.php
$Eagle = array();
while($row = mysqli_fetch_array($result))
{
$Eagle[] = "#".$row[numb]." ".$row['FName']." ".$row['LName']." ".$row['Date'];
}
Concatenate your $Eagle array to a string using implode() (php docs) and put it into an <marquee> element
echo '<marquee behaviour="scroll">' . implode(' | ', $Eagle) . '</marquee>';
for more options, have a look here or here
EDIT
an alternative way of doing what you're trying to accomplisch:
echo '<marquee behaviour="scroll">';
while ($row = mysqli_fetch_array($result))
echo "#{$row[$numb]} {$row['FName']} {$row['LName']} {$row['Date']}";
echo '</marquee>';

JQuery seems to be blocked by something else

JQuery seems to be blocked
Hello there, I've been confronting this problem for several days, I just can't find a way to get this fixed, or around it.
What I want to do is simple, I want to read out every sub-folder of a big Project folder. Then assign a thumbnail image and a figcapture to this folder. With a simple for loop, php builds this for me. Everything works perfect and quick. The only thing is that the jquery won't respond. Even though I have created various menus with this technique. As you can see in my code, in the "script" tags, I have the jquery code which doesn't seem to work.
I don't know wheter php puts in a space somewhere or I just looked too long at this code for seing the error.
I appreciate any help.
<?php
/*Because of the "ease of use" aspect of this site, I prefered to write it completely in PHP,
advantage of that:
Images are loaded directly out of the folder, so I can just drag and drop something onto the server without having to write aditional code.
As you see, it can save a lot of time.*/
echo "<h1>Referenzen</h1><br>";
$projects = scandir('../src/sub/credentials'); //The credentials directory is being scanned and converted into an array.
$projectsSize = count($projects); //$size is being created. It counts the number of objects in the array which has been created above.
$projectsCaptions = file('../src/sub/captionsOfCredentials.txt'); //Edit the name of the figcaption in the "captionsOfCredentials.txt".
for($i = 2; $i < $projectsSize; $i++){ /*Simple "for" loop, that creates $i with the size of two, because PHP is 0-index based and has the "dot" and the "dotdot" folder. The loop stops at the end of the array($size).*/
echo '<a href="index.php#PRJ'.trim($projectsCaptions[$i]).'" class="ID'.trim($projectsCaptions[$i]).'">
<div class="projectFolder">
<img src="src/sub/credentialsThumb/project_00'.$i.'.jpg" width="100%" />
<figcaption>'
.$projectsCaptions[$i].
'</figcaption>
</div>
</a>';
/*Project folder level starts here.*/
$images = scandir('../src/sub/credentials/project_00'.$i);
$imagesSize = count($images);
for($k = 3; $k < $imagesSize; $k++){
$tempID = ('ID'.trim($projectsCaptions[$i]).'.php'); //$tempID is the entire file name including the .php part.
$handle = fopen($tempID, "a") or die ('Unable to open '.$tempID.' , please contact admin A.S.A.P..');
$imagesCode = 'test';
fwrite($handle, $imagesCode);
}
//end second for-loop
echo "
<script>
$(document).ready(function () {
$('#ID".$projectsCaptions[$i]."').click(function () {
$('#mainContent').load('de-DE/".$tempID."');
});
});
</script>";
}
//end first for-loop
?>
You're selecting an element by id when you need to use class. Change the JS block to this:
$(document).ready(function () {
// Note ".ID" not "#ID"
$('.ID".$projectsCaptions[$i]."').click(function () {
$('#mainContent').load('de-DE/".$tempID."');
});
});
UPDATE
It seems like you've also got an illegal character in $projectsCaptions[$i]. It's most likely a newline character. Try wrapping the above reference above in trim():
$('.ID" . trim($projectsCaptions[$i]) . "').click(function () {

Categories

Resources