0

Context:

I'm trying to make an automatically generated list that has a different amount of items on each page. Each item needs a name and a link, based on its place on the list. Here's an example with two items for the problematic part:

PHP variables:

$item_link_1 = "link1";
$item_name_1 = "first item";
$item_link_2 = "link2";
$item_name_2 = "second item";

jQuery:

$("#items").each(function(i) {
$(this).find("a").attr("href", "<?php echo $item_link_"+ ++i +";?>");
});
$("#items").each(function(i) {
$(this).find("a").text("<?php echo $item_name_"+ ++i +";?>");
});

HTML output:

<div id="items">
<a href="link1" class="item-1">first item</a>
<a href="link2" class="item-2">second item</a>
</div>

Problem:

Obviously, $item_name_"+ ++i +"; will never work. I need a way to add the number generated by jQuery to the end of the variable and echo this new variable. So, if it's the 4th item, the variable will be $item_name_4. The value of this variable (set manually) will be displayed by jQuery with the text() function.

At least that's my idea for how to automate the process. If there's a way to do this, please tell me. If you know a better way, please tell me.

4
  • 1
    Why do you need to generate the numbers in jQuery? You can do all that directly in php. Commented Jun 18, 2015 at 18:33
  • Because that's the first way of doing it that I found. How I said, better ways of doing this are welcome. Now, how can I do those in php? Commented Jun 18, 2015 at 18:36
  • <?php echo $item_link_" . $i++; ?> Commented Jun 18, 2015 at 18:36
  • Loops and string concatenation are pretty basic features in any language. If you try it yourself, you probably end up with better code than the things that you found. Commented Jun 18, 2015 at 18:38

1 Answer 1

4

The problem your having is the difference between server-side processing and client-side processing.

An easy way to think about this is that PHP is handled before the HTML is even put on the screen replacing all the PHP parts with their variable contents. meaning that adding that php text with a client-side language like javascript won't be able to execute the php code and achieving the results you're after.

Luckily your example can be done explicitly in php:

<?php
$links = array(
    array('link' => "Link1", 'name' => "first item"),
    array('link' => "Link2", 'name' => "second item")
);
?>
<div id="items">
    <?php for($i = 0; $i < count($links); $i++){ ?>
        <a href="<?php echo $links[$i]['link']; ?>" class="item-<?php echo $i; ?>"><?php echo $links[$i]['name']; ?></a>
    <?php } ?>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

tried that, and all I got was <div id="items"> <a href="" class="item-0"></a> <a href="" class="item-1"></a> </div>
edited, my bad. I changed loop types when I was writing this.
I'll need some time to apply this to my thing, but I think you just shortened my 50-lines-long script to something simple. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.