Dynamic PHP variables for jQuery use - php

I'd like to apply jQuery to classes that are generated dynamically as they are looped through PHP/MySQL.
I've got a non-dynamic working version on JSfiddle: jsfiddle.net/TXJA5/1/
In the example, cat_fruit, cat_vegetable and cat_cereal would be echoed within a PHP loop; something like:
<div class="category cat_<?php echo $category; ?>">
<p><?php echo $category_label; ?></p>
</div>
Then, in following columns, there will be divs containing items associated to the category:
<div class="column">
<div class="item cat_<?php echo $category; ?>">
<p><?php echo $item[0]; ?></p>
<p><?php echo $item[0]; ?></p>
</div>
</div>
So, why? Well, while the data is variable and "categories" exist in one column and "items" exist within other columns in another div, I need the associated divs to be identified by jQuery to make them the same height (using equalizeCols — see the fiddle; you'll get it.)
n.b. There's a reason this isn't a table, despite the obvious column/row relationships. I considered it (fretfully) and tried it, but it won't work for the needs of the actual project.
Thanks in advance for any help you can offer. This is my first question on SO after years of learning from it — this community is awesome. I've found a couple questions on here that may make mine look like a duplicate, but they couldn't quite help get me there!

<div class="column">
<div class="category" data-cat="<?php echo $category; ?>">
<p><?php echo $item[0]; ?></p>
<p><?php echo $item[0]; ?></p>
</div>
</div>
I guess you can add a data atribute (or an id) in the first column and then loop through those with jQuery:
$('.category').each(function(){
var category = $(this).attr('data-cat');
$(".cat_"+category).equalizeCols();
});
http://jsfiddle.net/TXJA5/2/

You can loop categories in the same way you do in the HTML part of the code:
<?php for(...){ ?>
var $els = $(".cat_<?php echo $category?>").equalizeCols();
<?php } ?>
If I understand your question and issue OK this is the solution. If this is not your issue please be more specific.

Related

Create CSS Cards from PHP array?

Anyone knows how to do create multiple cards looping through a PHP array?
For example, I have 5 friend with 5 description corrresponding each friend (from a mysqli table) saved in $friendList.
So I want, for each row, to create and show a card with the friend as a header and its description as the content of the card.
This would be the loop
while ($row = mysqli_fetch_array($friendList, MYSQLI_ASSOC)){
// $row['friend'];
// $row['description'];
}
but then, I do not know how to create the cards with the variables obtained:
Assuming that you have $friendList variable already defined and it is a mysqli_result object, here's how you can do it:
<?php while($row = mysqli_fetch_array($friendList, MYSQLI_ASSOC)): ?>
<div class="w3-card-4 test">
<img src="img_avatar3.png" alt="Avatar">
<div class="w3-container">
<h4><b><?php echo $row["friend"] ?></b></h4>
<p><?php echo $row["description"] ?></p>
</div>
</div>
<?php endwhile; ?>
Feel free to ask any questions :-)

Echo an image tag with site_url() inside PHP tags

I have a loop in my view that outputs all the content gathered from the database:
<?php foreach($content as $contentRow): ?>
<?php
echo $contentRow->value;
?>
<?php endforeach; ?>
This works fine for HTML strings like:
<h2><strong>Example Text</strong></h2>
however I have some image content that I would like to display and I have tried the following database entries to no avail:
<img src="<?php echo site_url('pathToImage/Image.png'); ?>" alt="Cover">"
<img src="site_url('pathToImage/Image.png')" alt="Cover\">"
I feel like I am missing a step on how to use PHP values in this way.
How do I access the URL of the image and use that to show the image?
Full Code Edit
<?php
$CI =& get_instance();
?>
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col-md-2"></div>
<div class="col-md-20">
<!--<form class="form-center" method="post" action="<?php echo site_url(''); ?>" role="form">-->
<!-- <h2 class="">Title</h2>
<h2 class=""SubTitle/h2>-->
<?php echo $this->session->userdata('someValue'); ?>
<!--//<table class="" id="">-->
<?php foreach($content as $contentRow): ?>
<tr>
<td><?php
echo $contentRow->value;
?></td>
</tr>
<?php endforeach; ?>
<!--</table>-->
<!--</form>-->
</div>
<div class="col-md-2"></div>
</div>
</div>
</div><!-- /.container -->
and the values are being read out in $contentRow->value;
I have to verify this, but to me it looks like you are echo'ing a string with a PHP function. The function site_url() is not executed, but simply displayed. You can execute it by running the eval() function. But I have to add this function can be very dangerous and its use is not recommended.
Update:
To sum up some comments: The use of eval() is discouraged! You should reconsider / rethink your design. Maybe the use of tags which are replaced by HTML are a solution (Thanks to Manfred Radlwimmer). Always keep in mind to never trust the data you display, always filter and check!
I'm not going to accept this answer as #Philipp Palmtag's answer helped me out alot more and this is more supplementary information.
Because I'm reading data from the database it seems a sensible place to leave some information about what content is stored. In the same table that the content is stored I have added a "content type" field.
In my view I can then read this content type and render appropriately for the content that is stored. If it is just text I can leave it as HTML markup, images all I need to do is specify the file path and then I can scale this as I see fit.
I have updated my view to something akin to this and the if/else statement can be added to in the future if required:
<?php foreach($content as $contentRow): ?>
<?php if ($contentRow->type != "image"): ?>
<?php echo $contentRow->value; ?>
<?php else: ?>
<?php echo "<img src=\"".site_url($contentRow->value)."\">"; ?>
<?php endif; ?>
<?php endforeach; ?>

How can I display the value of a field in WordPress?PHP

I have this site:
link
I installed the plugin Types and tried this code to display the value field in a div.
<div class="selectat">
<div>
<?php $variable = do_shortcode("[types field='descriere' ]");?>
<?php echo $variable;?>
</div>
</div>
My field is called descriere..I put a picture to see more clearly.
Unfortunately this blank although I do not see anything on the site and basically my div is empty ..
It is wrong the way I wrote the code?
I must call another function?
You can help me solve this problem please?
Thanks in advance!
You dont need to use any shortcode you could use
<div class="selectat">
<div>
<?php $variable = the_field( 'descriere' );
<?php echo $variable;?>
</div>
</div>
But if you want to echo the code straight away you can just use get_field('') which echos the value straight to the page.
<div class="selectat">
<div>
<?php $variable = get_field( 'descriere' );
</div>
</div>

php if statement not evaluating properly

This is my first time posting a question here, it has been a valuable resource before so I thought maybe someone would be able to answer my question.
It might be a simple solution, but as I said already, I'm a PHP noob. Forgive me.
Here is my code:
<?php wp_reset_postdata(); // reset the query ?>
<?php $authorposts = get_the_author_posts();
if ($authorposts < 1) {
echo " ";
}
else { ?>
<div class="single-sidebar"><!--expert_blog start-->
<div class="single-sidebar-middle">
<div class="single-sidebar-top">
<h3>More Posts by <?php the_author();?></h3>
</div>
<p><?php echo get_related_author_posts(); ?></p>
<div class="single-sidebar-bottom">
<div class="more_blog">+ Read more</div>
</div>
</div>
</div>
<div class="clear"></div>
<?php wp_reset_postdata(); // reset the query ?>
What this is supposed to do: get the number of posts by a particular author (of the post currently being viewed), and output it into a conditional statement saying if it is 1 or less than 1, display nothing. Otherwise, display the titles of each post.
What happens: The divs show up even when there is only one post attirbuted to the author, but nothing shows up in the list (im guessing because there is only one post and you are already viewing it).
Any help would be appreciated, it is late and my brain has melted. Please let me know if anything needs clarification.
Thanks in advance.
Instead of if ($authorposts < 1) it should be if ($authorposts <= 1)

cakephp 1.3, how to use Paginator->sort() for two columns?

i have a simple application where i view some posts from a database and i use Paginator->sort to sort.
What i did is i duplicate everything so that i show the posts twice as well as the sorting.
something like this:
link_sort
post1
post2
post3
link_sort
post1
post2
post3
But when i click link_sort both post display get sorted, and i could see that because they are the same. But how can i make it unique so that one link sorts one post display and another one the other one :).
here is some code:
<div class="sort"><?php echo $this->Paginator->sort('title');?></div>
<div class="sort"><?php echo $this->Paginator->sort('body');?></div>
<div class="content_title"><?php echo $post['Post']['title']; ?></div>
<div class="content_body"><?php echo $post['Post']['body']; ?></div>
<div class="sort"><?php echo $this->Paginator->sort('title');?></div>
<div class="sort"><?php echo $this->Paginator->sort('body');?></div>
<div class="content_title"><?php echo $post['Post']['title']; ?></div>
<div class="content_body"><?php echo $post['Post']['body']; ?></div>
any ideas?
thanks
I think this will work -
create two different variables in the controller:
$paginate1 = $this->paginate('conditions...')
$paginate2 = $this->paginate('conditions...')
if am not mistaken I had a similar problem once and this is how I solved it.

Categories