Integrating articles to a slider

Asked

Viewed 70 times

0

I’m working on an article slider, whose articles are in a database that can be eleminated/added (CMS), everything is working perfectly, the database, and the slider. Now I just wanted to integrate the articles into the slider. Here’s my code, what’s happening is that the articles are all coming out at once on top of each other, some hint?

JS:

sliderInt=1;
sliderNext=2;
$("#newsText > p#1").fadeIn(500);
startSlider();

function startSlider() {
    count = $("#newsText > p").size();
    loop = setInterval (function() {
        if (sliderNext>count) {     
            sliderNext = 1;
            sliderInt = 1;
        }

    $("#newsText > p").fadeOut(500);
    $("#newsText > p#" + sliderNext).delay(500).fadeIn(500);
    sliderInt = sliderNext;
    sliderNext ++;

    } ,7500);
}

function stopLoop() {
    window.clearInterval(loop);
}

function showSlide(id) {
    stopLoop();
        if (id>count) {       
            id = 1;
      }
        else if (id<1) {
            id=count;
      }

    $("#newsText > p").fadeOut(500);
    $("#newsText > p#" + id).fadeIn(500);

    sliderInt = id;
    sliderNext = id + 1;
    startSlider();
}
})

PHP:

<?php
include_once('php/CMS/includes/connection.php');
include_once('php/CMS/includes/article.php');

?>
.......HTML....
<?php 

                for ($id=1; $id < count($articles); $id++) {
                    echo ("<p id=".$id.">".$articles[$id]['article_content']."</p>");
                }

            ?>
  • I’ve updated the question above... what’s happening is that the articles are all appearing at the same time on top of each other

  • It seems to me that Javascript is having the slideshow start before the slides have been loaded. Have you tried placing Javascript at the bottom of the page or calling the function startSlider(); within a $(document).load(callback)?

  • Javascript is at the end, and if there are static p tags with ids numbered from 1 works very well, I think the error is in php. Obgado

  • Now that I’ve noticed, why are you assigning the same id on all Divs? By definition, you cannot have ids with the same name on a page. Take that foreach that is outside the for and use only the for in the names of ids.

1 answer

0


Your problem is in this code snippet:

foreach ($articles as $article) {
    for ($articles=0; $articles <= count($articles); $articles++) {
         $id = 1;
         echo ("<p id =".$id++.">".$article['article_content']."</p>");
    }
}

Take off the foreach that is outside the for and use only the for to increase the ids of divs. By definition, you cannot have elements with the same id in an HTML page.

Suggestion for correction:

$article = new Article;
$articles = $article->fetch_all();
for ($i=0, $count=sizeof($articles); $i<$count; $i++) {
     echo ("<p id='article_".$i."'>".$articles[$i]['article_content']."</p>");
}
  • But if I take out the foreach gives error (Cannot use Object of type Article as array in C: Webs...), can not identify

  • @Miguel gives error because you are declaring a variable $article at the beginning of the code. That’s unnecessary, take that line and the foreach, leaving only the for. I made a suggestion for correction, take a look.

  • It didn’t work because the Article class is fetching $article['article_content'] from the mysql database. The problem is not to give error, I believe it is only logical in the php part, because the articles appear, but all appear at the same time

  • @Miguel It’s true, I had not noticed the bass line. As I said, the problem is that all the elements were with the same id. Put the correction suggestion I gave and try again :)

  • It gave "Undefined offset: 4 in C: Webs..." on the echo line. I did your update on top

  • I took the "<p id='article_". $i..." and left only the "<p id = . $i...." is already working but the error continues on the echo line, although the slider is already perfect

  • @Miguel I made one more correction in the code: it is not to have one <= in the for, only one <. (if your article array has 4 elements, the for should go from 0 to 3; element 4 would be undefined). If it worked, don’t forget to ok the answer :)

  • Thank you so much @Rodrigo

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.