4

We have a bunch of data in a MySQL database which call by using this phpcode:

$query = "SELECT * FROM `dmt_objects` WHERE PipelineAndWorkflow = 'RWP WF  Ready for 4Sprint' ORDER BY `Team` DESC  ;";
                        $select_projects = mysql_query($query);
$project_array = mysql_fetch_array($select_projects);
                        $json_project_array = json_encode($project_array);

However, we would no like to create one div for each team. If its ten teams we want to creat ten divs. We tried something like this in our HTML:

<script type="text/javascript">

var project_array = $json_project_array;

number_of_teams = project_array.length;

for(var i = 0; i < number_of_teams; i++){ 

var iDiv = document.createElement('div');
iDiv.className = 'project_array['Team']';
                }
</script>

However, it don't seam to work, even if put document.write(i); in the for loop instead of trying to creat a div.

3
  • you can use php to add div Commented Sep 11, 2015 at 8:53
  • okay, right, how do I do that? Commented Sep 11, 2015 at 8:54
  • 1
    You can't do var project_array = $json_project_array;. You're trying to do straight PHP inside javascript tags, without declaring it as PHP. You would atleast need to echo out the $json_project_array where you are using it (<?php echo $json_project_array; ?>), but what @raveenanigam has suggested is a better way to do it (in my opinion). Commented Sep 11, 2015 at 8:57

2 Answers 2

1

I believe this is the code that will give you the output you desire...

$query = "SELECT * FROM `dmt_objects` WHERE PipelineAndWorkflow = 'RWP WF  Ready for 4Sprint' ORDER BY `Team` DESC  ;";
$select_projects = mysql_query($query);

if (mysql_num_rows($select_projects) > 0) {
    while($row = mysql_fetch_assoc($select_projects)) {
        echo '<div>'.$row['Team'].'</div>';
    }
}

Also, do yourself a favor and change your code to pdo_mysql. mysql has been deprecated and will soon be removed. When this happens your code will stop functioning completely.

Reference

pdo_mysql

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

Comments

0

Well you can try this :

$query = "SELECT * FROM `dmt_objects` WHERE PipelineAndWorkflow = 'RWP WF  Ready for 4Sprint' ORDER BY `Team` DESC  ;";
$select_projects = mysql_query($query);

if (mysqli_num_rows($select_projects) > 0) {

while($row = mysqli_fetch_assoc($select_projects)) {

    echo '<div class="'.$row[yourcolumnname].'"></div>';
}

}

2 Comments

Great! However his code created multipel divs for each team. In the database, one team can be connected to multiple objects and for each object it is connected to, we now create a div...
Fixed it by addning '$team = array();' and 'if($row['Team'] !== "" && (in_array($row['Team'], $team) == False)){ echo '<div class="Teams" id="'.$row['Team'].'"></div>'; array_push($team, $row['Team']); }'

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.