0

I have a bit difficulty to explain this question in a simple way, so explanation will be long. User can vote if comment is useful or not (simple yes or no), and when he do this, his vote is send into data base -> table 'user_vote'.

Table user_vote have two columns - user_id and container. Every user have only one row in this table. When he cast new vote, field container is updated. For example, user vote on a comment with id 205 and he voted yes, and on a comment with id 300 he voted no. Table user_vote will look like this:

user_id->111;

container->205-yes,300-no

This is working fine, until this code here, where I need to check if user had voted on particular comment. Code here is working partially - problem is following:

Is user had voted 3 time and if id of the comment matches $split[0] it will one time write $text and it will produce 2 set of buttons, and if id of the comment is not matched it will produce 3 set of buttons (part after elseif is working correctly).

What am I doing wrong and how can I write code in correct way?

<div class="zuta_strana_komentar_korisno" data-id="<?php echo $cmm['id_comment'] ?>">
    <?php if(isset($glas) && !empty($glas)) : 
    $br = explode(',', $glas->container);
    foreach($br as $br) :                                  
    $split = explode('-', $br);
    if($split[0] == $cmm['id_comment']) : ?>
    <?php 
    if($split[1] == 'yes') :
    $text = 'koristan.';
    else :
    $text = 'nekoristan.';
    endif;
    ?>
    <p>Ovaj komentar vam je bio <?php echo $text ?></p>
    <?php else :?>
    <p>Da li vam je ovaj komentar bio od pomoći?</p>
    <button role="yes">Da</button><button role="no">Ne</button>
    <?php 
    endif;
    endforeach;             
    elseif($this->session->userdata('is_logged_in') == TRUE && empty($glas)) : ?>
    <p>Da li vam je ovaj komentar bio od pomoći?</p>
    <button role="yes">Da</button><button role="no">Ne</button>
    <?php endif ?>
    </div>
1
  • I think the problem is in foreach($br as $br) : change this to something foreach($br as $br_value) : - then you'll also need to change all the $br to $br_value inside this loop. Commented Aug 14, 2012 at 10:34

3 Answers 3

1

You are fundamentally designing your database improperly. Good database design does not store information using comma separated lists in fields. By storing the data in the comma separated lists, you are essentially nullifying all the database's querying functionalities. Use tables, that's why you're given them!

I suggest creating tables: users, comments, and votes

users table contains id, username... among other fields

comments table contains id, body, user_id...among other fields

votes table contains id, comment_id, user_id, yes_or_no... among other fields

Then get a handle on how to properly use SQL Joins

Sign up to request clarification or add additional context in comments.

2 Comments

I managed to solve this problem, but I found your advice very useful. Idea behind the way I designed DB is to have one small table -> one user using only one row. I want to escape situation where one user had voted (for example) million times. If every vote have unique id, it will create million rows - just for one user, and if there are a lot of users, DB will be very, very big. I don't have training in designing DB's, so I am using my own logic, and my logic is not always logical :D. Thank you for advice :).
My pleasure. Guard your database writes with PHP; Before you perform a write to the votes table, first count how many votes the user already has on that specific comment. If the count is too high, reject the write and send user appropriate error message, otherwise accept the write. This will keep the size of your votes table low (in a relative sense).
0

All you have to do is change

$br = explode(',', $glas->container);
foreach($br as $br) :                                  

to

$br_lines = explode(',', $glas->container);
foreach($br_lines as $br) :                                  

But you really need to separate your PHP logic from the HTML first. It looks so god awful my eyes want to bleed.

1 Comment

This solution doesn't work (everything works the same way).It have same effect on my eyes, but until I figure it out, it will stay this way.
0

Try this code:

<?php if(isset($glas) && !empty($glas)) {
    $br = explode(',', $glas->container);
    foreach($br as $b) {                                  
       $split = explode('-', $b);
       if($split[0] == $cmm['id_comment']) {
       ?>
       <p>Ovaj komentar vam je bio <?php echo ($split[1] == 'yes')?'koristan.':'nekoristan.'; ?></p>
       <?php } else {?>
       <p>Da li vam je ovaj komentar bio od pomoći?</p>
       <button role="yes">Da</button><button role="no">Ne</button>
       }
    }
}

Comments

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.