divide php mysql result left and right - php

$flag=0;
if($q->num_rows > 0) :
echo '<div id="testimonial">';
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
if($flag=0) :
$class=test1; $flag=1;
else :
$class=test2; $flag=0;
endif;
echo '<div class="'.$class.'">';
echo '<span class="left">';
echo '<p>'.$r['compname'].'</p>';
echo '<p>'.$r['position'].'</p>';
echo '</span>';
echo '<span class="right">';
echo '<p>'.$r['testimonial'].'</p>';
echo '</span>';
echo '</div>';
endwhile;
echo '</div>';
else :
echo '<h1>Coming Soon</h1>';
endif;
i want the result look like the picture! seems my php code doesnt work out the css class. its only showing 1 class test1 when i echoing the result. so all the result left align.

if($flag=0)
^ should be ==
This is easier though with:
$i = 0;
while (…) {
$class = $i++ % 2 ? 'test1' : 'test2';
}

You have an assignment in your if statement instead of a comparison: if($flag=0).

$class=test1; $flag=1;
^----^---
you're missing some quotes there too. As it stands now, you're trying to assign a constant named "test1" and "test2", which are most likely not defined, so they'll evaluate to an empty string.

$flag=0;
if($q->num_rows > 0) :
echo '<div id="testimonial">';
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
if($flag==0) :
$class='test1'; $flag=1;
else :
$class='test2'; $flag=0;
endif;
echo '<div class="'.$class.'">';
echo '<span class="left">';
echo '<p>'.$r['compname'].'</p>';
echo '<p>'.$r['position'].'</p>';
echo '</span>';
echo '<span class="right">';
echo '<p>'.$r['testimonial'].'</p>';
echo '</span>';
echo '</div>';
endwhile;
echo '</div>';
else :
echo '<h1>Coming Soon</h1>';
endif;
This should work!

Related

Swapping between two div classes in foreach

I am currently coding a page based on a json query.
Here is a snippet of the code, I don't want to give the top half as that is grabbing the remote json with id's and passwords, but here is the main snippet:
$content_size = 'one_half';
foreach ( $json_decoded['fixtures-results']['matches'] as $match ){
display_match($match);
}
function display_match($match){
global $content_size;
if (strtotime($match['date']) < time()) { goto skip; }
$home_team_name = strtolower($match['home-team']['name']);
$home_team_name = preg_replace('/\s+/', '_',$home_team_name);
$away_team_name = strtolower($match['away-team']['name']);
$away_team_name = preg_replace('/\s+/', '_',$away_team_name);
$match_date = date("M jS, Y", strtotime($match['date']));
echo '<div class="'.$content_size.'">';
echo '<div class="latest_results" style="background:#ffffff">';
echo '<h6>'.$match_date.', '.$match['status']['full'].'</h6>';
echo '<div class="latest_results_col"><img src="/team_logos/'.$home_team_name.'.png"><span>'.$match['home-team']['name'].'</span></div>';
echo '<div class="latest_results_col goalscore2">vs</div>';
echo '<div class="latest_results_col"><img src="/team_logos/'.$away_team_name.'.png"><span>'.$match['away-team']['name'].'</span></div>';
echo '<div class="clear">';
echo '<span>Location:'.$match['venue'].'</span>';
echo '</div>';
echo '</div>';
echo '</div>';
if ($content_size == "one_half") { $content_size = "one_half_last"; goto skip; }else{ $content_size = "one_half"; goto skip; }
skip:
}
What i want is the first div class to be one_half then the next one as one_half_last then back to one_half and so on.
The result is the first div has the class one_half but the second has no class, then the third has one_half and so on, so you see it seems to be working, except its missing the second, fourth, sixth ect class.
Any help would be greatly received.
Kind Regards,
Adam
I am not sure what happened, and why it corrected itself, but here is the code i ended up using.
$content_size = 'one_half';
foreach ( $json_decoded['fixtures-results']['matches'] as $match ){
display_match($match);
}
function display_match($match){
global $content_size;
if(!$content_size){ $content_size = "one_half"; }
if (strtotime($match['date']) < time()) { goto skip; }
$home_team_name = strtolower($match['home-team']['name']);
$home_team_name = preg_replace('/\s+/', '_',$home_team_name);
$away_team_name = strtolower($match['away-team']['name']);
$away_team_name = preg_replace('/\s+/', '_',$away_team_name);
$match_date = date("M jS, Y", strtotime($match['date']));
$home_team_name_2 = team_name_2($match['home-team']['name']);
$away_team_name_2 = team_name_2($match['away-team']['name']);
echo '<div class="'.$content_size.'">';
echo '<div class="latest_results" style="background:#ffffff">';
echo '<h6>'.$match_date.', '.$match['status']['full'].'</h6>';
echo '<div class="latest_results_col"><img src="/team_logos/'.$home_team_name.'.png"><span>'.$home_team_name_2.'</span></div>';
echo '<div class="latest_results_col goalscore2">vs</div>';
echo '<div class="latest_results_col"><img src="/team_logos/'.$away_team_name.'.png"><span>'.$away_team_name_2.'</span></div>';
echo '<div class="clear">';
echo '<span>Location:'.$match['venue'].'</span>';
echo '</div>';
echo '</div>';
echo '</div>';
if ($content_size == "one_half") { $content_size = "one_half last_column"; goto skip; }else{ $content_size = "one_half"; goto skip; }
skip:
}
If anyone has any idea what I did differently I am happy to hear as I am not sure what i did.

foreach() Loop Outputting Data and Creating Layout

I am currently trying to create a class schedule which I pull from my Sql Server database with PHP and I am trying to get the layout output as well as the data as I am grouping the resources.
These groupings are nested such as:
-DAY
--TIME
---CLASS
----STUDENTS
And should output like this:
However, I am getting this:
My current output is working, however, it is only on the first loop, then everything goes haywire. I am assuming there is an erroneous </div> tag somewhere in my code yet I cannot for the life of me find it.
My php code is a function that is delcare as such:
<div class="mainScheduleWrapper">
<?php daySchedule(); ?>
</div>
My php code is as such:
function daySchedule() {
global $conn;
$dayScheduleQuery = 'SET DATEFIRST 1
SELECT [DAY].[DAY] AS [DAY], CLASS.CLASSTIME AS CLASSTIME, CLASSLEVEL.CLASSLEVEL AS CLASSLEVEL, CLASS.MAXSTUDENT AS MAXSTUDENT, INSTRUCTOR.FIRSTNAME AS INSTRUCTOR, STUDENT.FIRSTNAME AS STUDENTFIRST, STUDENT.SURNAME AS STUDENTLAST, STUDENT.DOB AS STUDENTDOB
FROM STUDENT JOIN BOOKING ON STUDENT.ID = BOOKING.STUDENTID JOIN CLASS ON CLASS.ID = BOOKING.CLASSID JOIN CLASSLEVEL ON CLASS.CLASSLEVELID = CLASSLEVEL.ID JOIN [DAY] ON CLASS.CLASSDAY = [DAY].ID JOIN INSTRUCTOR ON CLASS.INSTRUCTORID = INSTRUCTOR.ID
WHERE [DAY].ID = (DATEPART(dw, GETUTCDATE() AT TIME ZONE \'AUS Eastern Standard Time\'))
ORDER BY CLASS.CLASSTIME ASC, INSTRUCTOR.FIRSTNAME ASC, CLASSLEVEL.CLASSLEVEL ASC';
// COUNTERS
$t = 0;
$i = 0;
//VARIABLES FOR DAY SCHEDULE
$classDay = NULL;
$classTime = NULL;
$classInstructor = NULL;
$closeClass = false;
$closeAll = false;
$queryConnector = $conn->query($dayScheduleQuery);
foreach ($queryConnector as $schedule) {
// CLASS DAY HEADER
if ($classDay != $schedule['DAY']) {
echo '<div class="grid-1">';
echo '<h1>' . $schedule['DAY'] . '</h1>';
echo '</div><!-- Day closed! -->';
$classDay = $schedule['DAY'];
}
// CLASS TIME HEADER
if ($classTime != $schedule['CLASSTIME']) {
if($classTime != $schedule['CLASSTIME'] && $t > 0) {
$closeAll = true;
goto closeAll;
}
echo '<div class="grid-12-noGutter scheduleContainer">'; //NON-CLOSED
echo '<h1>' . 'T = ' . $t . '</h1>';
echo '<div class="grid-middle-center col scheduleTimeTab">';
// FIX 3 DIGIT MILITARY TIME
if (strlen($schedule['CLASSTIME']) < 4) {
$classScheduleTime = '0' . $schedule['CLASSTIME'];
} else {
$classScheduleTime = $schedule['CLASSTIME'];
}
echo '<p>' . date('g:i A', strtotime($classScheduleTime)) . '</p>';
echo '</div>'; //CLOSE TIME TAB
echo '<div class="innerSchedule">'; // NON-CLOSED
$classTime = $schedule['CLASSTIME'];
$t += 100;
}
// INSTRUCTOR HEADER
if ($classInstructor != $schedule['INSTRUCTOR']) {
if ($classInstructor != $schedule['INSTRUCTOR'] && $i > 0) {
$closeClass = true;
goto closeClassWrapper;
}
echo '<div class="classWrapper">';
echo '<h1>' . 'I =' . $i . 'T = ' . $t . '</h1>';
echo '<div class="grid-3-middle classHeader">';
echo '<div class="col classHeaderCell' . classLevelColour($schedule['CLASSLEVEL']) . '">' . $schedule['CLASSLEVEL'] . '</div>';
echo '<div class="col classHeaderCell">' . $schedule['INSTRUCTOR'] . '</div>';
echo '<div class="col classHeaderCell">Max' . ' ' . $schedule['MAXSTUDENT'] . '</div>';
echo '</div>';
echo '<div class="grid-4-middle" id="studentHeaders">';
echo '<div class="col"><h6>Student Name</h6></div>';
echo '<div class="col"><h6>Student Birthday</h6></div>';
echo '<div class="col"><h6>Class Level</h6></div>';
echo '<div class="col"><h6>Attendance</h6></div>';
echo '</div>';
$classInstructor = $schedule['INSTRUCTOR'];
$i += 100;
}
echo '<div class="grid-4 studentRow">';
echo '<div class="col">';
echo '<span class="studentCell">' . $schedule['STUDENTFIRST'] . ' ' . $schedule['STUDENTLAST'] . '</span>';
echo '</div>';
echo '<div class="col">';
echo '<span class="studentCell">' . $schedule['STUDENTDOB'] . '</span>';
echo '</div>';
echo '<div class="col">';
echo '<span class="studentCell">' . $schedule['CLASSLEVEL'] . '</span>';
echo '</div>';
echo '<div class="col">';
echo '<span class="studentCell">--</span>';
echo '</div>';
echo '</div>';
// GOTO TAGS
closeClassWrapper: {
if ($closeClass === true) {
echo '</div>';
$closeClass = false;
$i = 0;
}
}
closeAll: {
if ($closeAll === true) {
echo '</div>';
echo '</div>';
echo '</div>';
$closeAll = false;
$t = 0;
$i = 0;
}
}
}
}
Any help would be greatly appreciated - even if it's to tell me I'm going about it the completely wrong way.
Kindest Regards
Michael Z
I wouldn't say you're going about it the completely wrong way, but a few red flags jumped out at me in your code.
The use of goto jumping is bad practice. It butchers your program flow and forces you to segregate tasks that shouldn't be kept apart. You marked the sections of code "// NON CLOSED" when there was a </div> missing, is there any purpose for that? How do you know the goto sections are reliable?
When you echo something like <div class="col">, without escaping the double-quotes (as in \" for every " character), it can be problematic. Your code can get mangled or misinterpreted, both on the PHP end or on the HTML end.
Like others have said, the use of PHP may be overkill here. Besides just sending the JSON, the rest could be handled with JavaScript.

Show more than one result from JOIN with if statement

I have this JOIN, that gets me more than on result:
at.tipo AS atividade,
at.data AS datacadastro,
at.user AS usercadastro,
LEFT JOIN atividades at
ON (at.produto = '$produto')
AND (at.tipo = 'cadastra' OR at.tipo = 'revisado')
And I have: if string = 'cadastra' shows something, if string = 'revisado' shows something else.
<?php
if ($row['atividade'] == 'cadastra') {
echo '<div id="user-img">';
echo '<img src="http://'.$row['imguser'].'"/></div>';
echo '<div id="user-cadastro" class="greytxt">';
echo 'Cadastrado por <br>' .$row['usercadastro']. '<br>em ';
echo $row['datacadastro']. '</div>';
}
?>
<?php
if ($row['atividade'] == 'revisado') {
echo '<div id="user-img">';
echo '<img src="http://'.$row['imguser'].'"/></div>';
echo '<div id="user-cadastro" class="greytxt">';
echo 'Cadastrado por <br>' .$row['usercadastro']. '<br>em ';
echo $row['datacadastro']. '</div>';
}
?>
The problem is, it's showing just the first result, and not all.
How can I display all results?
EDIT:
I have this JOIN inside a big query, that give me other important rows.
But I just want one result for this principal query (WHERE = $produto), and want more results from my JOIN table.
$rows = $result->num_rows;
for ($j = 0; $j < $rows; ++$j) {
$result -> data_seek($j);
$row = $result->fetch_array (MYSQLI_ASSOC);
?>
My result should be this, but JOIN shows just row 'cadastra'.
Use $result->fetch_array (MYSQLI_ASSOC) inside a while loop
while ($row = $result->fetch_array (MYSQLI_ASSOC)) {
if ($row['atividade'] == 'cadastra')
{
echo '<div id="user-img">';
echo '<img src="http://'.$row['imguser'].'"/></div>';
echo '<div id="user-cadastro" class="greytxt">';
echo 'Cadastrado por <br>' .$row['usercadastro']. '<br>em ';
echo $row['datacadastro']. '</div>';
} ?>
<?php if ($row['atividade'] == 'revisado')
{
echo '<div id="user-img">';
echo '<img src="http://'.$row['imguser'].'"/></div>';
echo '<div id="user-cadastro" class="greytxt">';
echo 'Cadastrado por <br>' .$row['usercadastro']. '<br>em ';
echo $row['datacadastro']. '</div>';
}
}

php- foreach statement duplicating results

I have a while and foreach loop that I want to use to print all of my results from a MySQL query. Below is my code for the while statement and the foreach statements.
WHILE:
while ($row_questions = mysql_fetch_array($result_questions)) {
$step_number[] = $row_questions['step_number'];
if ($row_questions['step_number_sub'] != 0) {
$step_number_sub[] = $row_questions['step_number_sub'];
} else {
$step_number_sub[] = "0";
};
if ($row_questions['step_required'] != 0) {
$step_required[] = "*";
} else {
$step_required[] = "";
};
$step_description[] = $row_questions['step_description'];
$step_outcome[] = $row_questions['step_outcome'];
$step_equipment[] = $row_questions['step_equipment'];
$test_changes[] = $row_questions['test_changes'];
};
and my FOREACH:
foreach ($step_number as $i => $step){
$even_odd = ( '-odd' != $even_odd ) ? '-odd' : '';
echo '<section class="zebra'.$even_odd.'">';
echo '<div class="span-2 number"><strong>'.$step_number[$i].'</strong></div>';
echo '<div class="span-20 description">'.$step_description[$i].'</div>';
echo '<div class="span-2">'.$step_required[$i].'</div>';
if ($step_outcome[$i] != null) {
echo '<div class="span-22 outcome"><strong>Desired Outcome</strong><br>'.$step_outcome[$i].'</div>';
}
echo '<div class="clear"></div>';
echo '<article class="results">';
echo '<div class="span-10">Did this step match the desired outcome?</div>';
echo '<div class="span-10">Notes:</div>';
echo '<div class="span-10">
<select name="question_'.$step[$i].'" id="question_'.$step[$i].'" required aria-required="true">
<option name="pass" value="Pass">Yes, this step was completed successfully</option>
<option name="fail" value="Fail">No, this step failed to complete. See notes below</option>
</select>
</div>';
echo '<div class="span-10"><textarea name="question_'.$step[$i].'" id="question_'.$step[$i].'" class="nots"></textarea></div>';
echo '</article>';
echo '<div class="clear"></div>';
echo '</section>';
};
I am getting multiple versions of the same results (4). Thanks in advance for any help and let me know if you need any other info!
Don't use multiple arrays. Use a class structure or array for each element. A simple way to do this:
while ($row_questions = mysql_fetch_array($result_questions))
questions[] = $row_questions;
foreach( $questions as $question )
{
$even_odd = ( '-odd' != $even_odd ) ? '-odd' : '';
echo '<section class="zebra'.$even_odd.'">';
echo '<div class="span-2 number"><strong>'.$question['step_number'].'</strong></div>';
echo '<div class="span-20 description">'.$question['step_description'].'</div>';
echo '<div class="span-2">'.$question['step_required'].'</div>';
if ($question['step_outcome'] != null)
{
echo '<div class="span-22 outcome"><strong>Desired Outcome</strong><br>'.$question['step_outcome'].'</div>';
}
... you get the drift ...
};

PHP Loop Question?

I have a php script that will loop until all the skill, exp and rating variables are displayed that the user entered into the database.
What I want is the following code to only display once and only if the variables are holding info and not empty. How can I do this. I hope I explained it okay.
I want this code displayed first.
echo '<div id="con">';
echo '<h2 id="s">Skills</h2>';
echo '<h2 id="exp">Exp</h2>';
echo '<h2 id="r">Rating</h2>';
Here is the full code.
<?php
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM skills WHERE user_id='3'");
if (!$dbc) {
print mysqli_error();
}
echo '<div id="con">';
echo '<h2 id="s">Skills</h2>';
echo '<h2 id="exp">Exp</h2>';
echo '<h2 id="r">Rating</h2>';
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['skill']) || !empty($row['exp']) || !empty($row['rating'])) {
if (! empty($row['skill'])) {
echo '<div class="s">';
echo '<p>' , htmlspecialchars($row['skill']) , '</p>';
}
if (! empty($row['exp'])) {
echo '<div class="s">';
echo '<p>' , htmlspecialchars($row['exp']) , '</p>';
}
if (! empty($row['rating'])) {
echo '<div class="s">';
echo '<p>' , htmlspecialchars($row['rating']) , '</p>';
}
}
}
echo '</div>';
?>
you can take the following after the loop:
echo '<div id="con">';
echo '<h2 id="s">Skills</h2>';
echo '<h2 id="exp">Exp</h2>';
echo '<h2 id="r">Rating</h2>';
and instead of echo in the loops , use vars to store the text and then check if the var holds something and then echo
update:
would become something like this:
$skills = "";
$exps = "";
$ratings = "";
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['skill']) || !empty($row['exp']) || !empty($row['rating'])) {
if (! empty($row['skill'])) {
$skills .='<div class="s">';
$skills .= '<p>' , htmlspecialchars($row['skill']) , '</p>';
}
if (! empty($row['exp'])) {
$exps .= '<div class="s">';
$exps .= '<p>' , htmlspecialchars($row['exp']) , '</p>';
}
if (! empty($row['rating'])) {
$ratings .= '<div class="s">';
$ratings .= '<p>' , htmlspecialchars($row['rating']) , '</p>';
}
}
}
echo '<div id="con">';
if($skills){
echo '<h2 id="s">Skills</h2>';
echo $skills;
}
if($exps){
echo '<h2 id="exp">Exp</h2>';
echo $exps;
}
if($ratings){
echo '<h2 id="r">Rating</h2>';
echo $ratings;
}
echo '</div>';

Categories