Writing PHP variable based on jQuery calculations and displaying predetermined value -


context:

i'm trying make automatically generated list has different amount of items on each page. each item needs name , link, based on place on list. here's example 2 items 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 +"; never work. need way add number generated jquery end of variable , echo new variable. so, if it's 4th item, variable $item_name_4. value of variable (set manually) displayed jquery text() function.

at least that's idea how automate process. if there's way this, please tell me. if know better way, please tell me.

the problem having difference between server-side processing , client-side processing.

an easy way think php handled before html put on screen replacing php parts variable contents. meaning adding php text client-side language javascript won't able execute php code , achieving results you're after.

luckily example can 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> 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -