Convert PHP-generated table layout to div - php

I'm building a site that uses a video plugin to generate a gallery of links to videos. The problem is that these images and links are put into a table, which is incompatible with the responsive design of my site. Being a web designer and not a PHP-developer, I'm at a loss when looking at this code about how to take out all the elements that generate the table and replace them with simple div containers, though it looks like it should be pretty straightforward for a PHP person.
<table id="media_galery">
<?php
$t = 0;
for ($i = 0; $i < count($cArray); $i++ ) {
$t++;
$file = $cArray[$i];
$remote = null;
$name = $file->getFileName();
$file_path = BASE_URL.DIR_REL.$file->getDownloadURL();
$ak_d = FileAttributeKey::getByHandle('media_date');
$date = $file->getAttribute($ak_d);
$ak_a = FileAttributeKey::getByHandle('media_artwork');
$image = $file->getAttribute($ak_a);
if($image==null){
$image_path=$uh->getBlockTypeAssetsURL($bt).'/tools/mp3.png';
}else{
$image = File::getByID($image->fID);
$image_path=$image->getURL();
}
$file_src = BASE_URL.DIR_REL.$file->getRelativePath();
$ak_s = FileAttributeKey::getByHandle('media_audio');
$audio = $file->getAttribute($ak_s);
if($audio){
$audio_array = $mh->getMediaTypeOutput($audio);
$audio_path = $audio_array['path'];
$file_src = $audio_path;
}
$video = null;
$ak_s = FileAttributeKey::getByHandle('media_video');
$video = $file->getAttribute($ak_s);
if($video){
$video_array = $mh->getMediaTypeOutput($video);
$video_path = $video_array['path'];
var_dump($video_array['id']);
$image_path = $mh->getMediaThumbnail($video_array['id'],$video_array['type']);
//$video_src = $video_path.'?width='.$width.'&height='.$height;
//$file_src = $video_src;
if($video_array['type'] == 'vimeo'){
$file_src = 'http://player.vimeo.com/video/'.$video_array['id'];
}else{
$file_src = $video_path;
}
}
?>
<td valign="top">
</a><img src="<?php echo $image_path?>" href="#popup_prep" alt="<?php echo $file_src?>" class="<?php echo $playerID?>player_get<?php echo $i?> gallery_thumb" href="<?php echo $file_src?>"/>
</td>
<?php
if($t == $spread){
$t=0;
echo '</tr>';
}
}
for($d=$t;$d<$spread;$d++){
echo '<td></td>';
if($t == $spread){
echo '</tr>';
}
}
?>
</table>
Any help on this would be very much appreciated!!

<div id="media_galery">
<?php
$t = 0;
for ($i = 0; $i < count($cArray); $i++ ) {
$t++;
$file = $cArray[$i];
$remote = null;
$name = $file->getFileName();
$file_path = BASE_URL.DIR_REL.$file->getDownloadURL();
$ak_d = FileAttributeKey::getByHandle('media_date');
$date = $file->getAttribute($ak_d);
$ak_a = FileAttributeKey::getByHandle('media_artwork');
$image = $file->getAttribute($ak_a);
if($image==null){
$image_path=$uh->getBlockTypeAssetsURL($bt).'/tools/mp3.png';
}else{
$image = File::getByID($image->fID);
$image_path=$image->getURL();
}
$file_src = BASE_URL.DIR_REL.$file->getRelativePath();
$ak_s = FileAttributeKey::getByHandle('media_audio');
$audio = $file->getAttribute($ak_s);
if($audio){
$audio_array = $mh->getMediaTypeOutput($audio);
$audio_path = $audio_array['path'];
$file_src = $audio_path;
}
$video = null;
$ak_s = FileAttributeKey::getByHandle('media_video');
$video = $file->getAttribute($ak_s);
if($video){
$video_array = $mh->getMediaTypeOutput($video);
$video_path = $video_array['path'];
var_dump($video_array['id']);
$image_path = $mh->getMediaThumbnail($video_array['id'],$video_array['type']);
//$video_src = $video_path.'?width='.$width.'&height='.$height;
//$file_src = $video_src;
if($video_array['type'] == 'vimeo'){
$file_src = 'http://player.vimeo.com/video/'.$video_array['id'];
}else{
$file_src = $video_path;
}
}
?>
<div class="img_div<?php if($t == ($spread + 1)){$t=0; echo ' first';}?>">
</a><img src="<?php echo $image_path?>" href="#popup_prep" alt="<?php echo $file_src?>" class="<?php echo $playerID?>player_get<?php echo $i?> gallery_thumb" href="<?php echo $file_src?>"/>
</div>
<?php
}
?>
</div>
I don't know what the value of $spread is, but with the code above you will output something like this:
<div id="meda_galery">
<div class="img_div">
...
<div>
<div class="img_div">
...
<div>
<div class="img_div first">
...
<div>
<div class="img_div">
...
<div>
</div>
The example above assumes a spread of 2 (so 2 columns). You could float each div left but use clear to start a new row for each one with a class of "first".

Related

How do I show just the first image from mysql database in php

I have the following code. How do I show the first image in the database with index of 0 for the large image display at end of the code? Right now it is showing the last image in the database.
<div id="imgWheel" class="treatmentContainer">
<?php
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$product = $row["product"];
$room = $row["room"];
$style = $row["style"];
$tags = $row["tags"];
$src = $row["url"];
$dataid = $row["id"];
$imgClass = "";
if (in_array($src, $favourites)) {
$imgClass = " favourite";
}
echo "<div class='treatment$imgClass' data-url='$src' data-product='$product' data-room='$room' data-style='$style' data-tags='$tags' data-number='$dataid' id='pic_$dataid' >";
echo "<img src='$src' crossorigin='anonymous'/>";
echo "</div>";
}
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $src ?>" />
</div>
Your result set is alreadyx ordered by id, so you need only a variable, to be filled once with the first imageurl
<div id="imgWheel" class="treatmentContainer">
<?php
$bigpictureurl = "";
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$product = $row["product"];
$room = $row["room"];
$style = $row["style"];
$tags = $row["tags"];
$src = $row["url"];
$dataid = $row["id"];
if (empty($bigpictureurl)) {
$bigpictureurl = $src ;
}
$imgClass = "";
if (in_array($src, $favourites)) {
$imgClass = " favourite";
}
echo "<div class='treatment$imgClass' data-url='$src' data-product='$product' data-room='$room' data-style='$style' data-tags='$tags' data-number='$dataid' id='pic_$dataid' >";
echo "<img src='$src' crossorigin='anonymous'/>";
echo "</div>";
}
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $bigpictureurl ?>" />
</div>
You just need to update your SQL query, just add LIMIT 1. This will limit the result just to 1 record and as you have ORDER id ASC, it will show the first record of the given user (as per you it is 0).
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id ASC LIMIT 1;";
A quick and dirty solution is to save your first image in some separate variables, for example like this:
$isFirst = true;
$firstImageSrc = "";
$result = ....;
while (...) {
// set your $product, $room etc here
if ($isFirst) {
$isFirst = false;
$firstImageSrc = $src;
}
}
echo ...
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $firstImageSrc ?>" />
</div>
A much more elegant solution would be to create an array with all your images, so that you can separate your php from your html. I will refactor your code below, and fix your first image problem as well:
<?php
$images = [];
$idx = 0;
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$images[$idx]["product"] = $row["product"];
$images[$idx]["room"] = $row["room"];
$images[$idx]["style"] = $row["style"];
$images[$idx]["tags"] = $row["tags"];
$images[$idx]["src"] = $row["url"];
$images[$idx]["dataid"] = $row["id"];
$images[$idx]["imgClass"] = "";
if (in_array($src, $favourites)) {
$images[$idx]["imgClass"] = " favourite";
}
$idx++;
}
?>
<div id="imgWheel" class="treatmentContainer">
<?php foreach ($images as $image) { ?>
<div class='treatment<?=$image["imgClass"]?>' data-url='<?=$image["src"]?>' data-product='<?=$image["product"]?>' data-room='<?=$image["room"]?>' data-style='<?=$image["style"]?>' data-tags='<?=$image["tags"]?>' data-number='<?=$image["dataid"]?>' id='pic_<?=$image["dataid"]?>' >
<img src='<?=$image["src"]?>' crossorigin='anonymous'/>
</div>
<?php } ?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?=$images[0]["src"]?>" />
</div>
Since you have all of that in your WHILE statement, I assume you want to echo all those records. And then at the end show the 1st pic. So for the "Large Image Display," give this a try:
<div id="display">
$query = "SELECT * FROM images WHERE user = 0;";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC)
$src = $row["url"];
<img id="mainImage" src="<?php echo $src ?>" />
</div>
If you'd like less code, then save the value of $src inside your WHILE loop when user=0 into some other variable like $src2. And then your code simply becomes:
<img id="mainImage" src="<?php echo $src2 ?>" />

Compare two arrays in PHP Laravel

I need to change the div's class name based on if condition below it.
i have tried as <div class="<?php echo $class_name; ?>">
I want the class name to be "class name 1" or "class name 2" based on a if condition like if submitted and correct answer are same, i want the class name to be class_name1 or else it should be class_name2. The code is as follows:
<element class="col-sm-6 col-aligncenter">
<!--<div class="pipeline white lined-success"> -->
<div class="<?php echo $class_name; ?>">
<div class="pipeline-header">
<h5>
<b><?php echo $count++;?>. <?php echo $question_type == 'fill_in_the_blanks' ? str_replace('^', '__________', $question_title) : $question_title;?></b>
</h5>
<span><?php echo get_phrase('mark');?>: <?php echo $mark;?></span>
</div>
<?php if ($question_type == 'multiple_choice'):
$options_json = $this->crud_model->get_question_details_by_id($row['question_bank_id'], 'options');
$number_of_options = $this->crud_model->get_question_details_by_id($row['question_bank_id'], 'number_of_options');
if($options_json != '' || $options_json != null)
$options = json_decode($options_json);
else $options = array();
?>
<ul>
<?php for ($i = 0; $i < $number_of_options; $i++): ?>
<li><?php echo $options[$i];?></li>
<?php endfor; ?>
</ul>
<?php
if ($row['submitted_answer'] != "" || $row['submitted_answer'] != null)
{
$submitted_answer = json_decode($row['submitted_answer']);
$r = '';
for ($i = 0; $i < count($submitted_answer); $i++)
{
$x = $submitted_answer[$i];
$r .= $options[$x-1].',';
}
} else {
$submitted_answer = array();
$r = get_phrase('no_reply');
}
?>
<i><strong>[<?php echo get_phrase('answer');?> - <?php echo rtrim(trim($r), ',');?>]</strong></i>
<br>
<?php
if ($row['correct_answers'] != "" || $row['correct_answers'] != null) {
$correct_options = json_decode($row['correct_answers']);
$r = '';
for ($i = 0; $i < count($correct_options); $i++) {
$x = $correct_options[$i];
$r .= $options[$x-1].',';
}
} else {
$correct_options = array();
$r = get_phrase('none_of_them.');
}
?>
<i><strong>[<?php echo get_phrase('correct_answer');?> - <?php echo rtrim(trim($r), ',');?>]</strong></i>
<?php elseif($question_type == 'true_false'):
if ($row['submitted_answer'] != "") {
$submitted_answer = get_phrase($row['submitted_answer']);
}
else{
$submitted_answer = get_phrase('no_reply');
}
?>
<i><strong>[<?php echo get_phrase('answer');?> - <?php echo get_phrase($submitted_answer);?>]</strong></i><br>
<i><strong>[<?php echo get_phrase('correct_answer');?> - <?php echo get_phrase($row['correct_answers']);?>]</strong></i>
**div's class should change based on if answer and correct answer are equal**
There are two types of questions (multiple choice and true or false). It should behave same for both type of questions.

Issues in my foreach PHP

Here's my problem, I have to display array values (description) under pictures like this :
Instead it appears like this
I'm working on this problem, this is my current code :
if (preg_match_all('/<div id="description" class="description">([^<]*)<\/div>/', $content, $match)) {
for( $i = 0; $i < count($match[0]); $i = $i+1 ) {
$description[] = $match[0][$i];
}
}
$attachments =& get_children($args);
$arrayMatches = array();
if ($attachments) {
foreach(array_chunk($attachments, 2) as $img) {
echo '<div class="two_cols">';
foreach($img as $attachment) {
foreach($attachment as $attachment_key => $attachment_value) {
$imageID = $attachment->ID;
$imageTitle = $attachment->post_title;
$imagearray = wp_get_attachment_image_src($attachment_value, $size, false);
$imageAlt = get_post_meta($imageID, '_wp_attachment_image_alt', true);
$imageURI = $imagearray[0]; // 0 is the URI
$imageWidth = $imagearray[1]; // 1 is the width
$imageHeight = $imagearray[2]; // 2 is the height
?>
<div class="col_1_2">
<div id="attachment_<?php echo $imageID; ?>" class="wp-caption alignnone" style="width: 356px;">
<a rel="lightbox-0" href="<?php echo $imageURI; ?>"><img class="wp-image-<?php echo $imageID; ?> size-full" title="<?php echo $imageTitle; ?>" src="<?php echo $imageURI; ?>" alt="<?php echo $imageAlt; ?>" width="456" height="304" /></a>
<p class="wp-caption-text"><?php echo $imageTitle; ?></p>
<?php
$arrayMatches[] = $match[0][$j];
?>
</div>
</div>
<?php
break;
}
$j++;
}
foreach(array_chunk($arrayMatches, 2) as $desc) {
echo '<div class="description">';
foreach($desc as $item) {
echo $item;
}
echo '</div>';
}
echo '</div>';
}
}
I try so many solutions but no one was the good one.
Thanks for the help ;)
Try:
foreach(array_chunk($arrayMatches, 2) as $k => $desc) {
if($k < 1) continue;
echo '<div class="description">';
foreach($desc as $item) {
echo $item;
}
echo '</div>';
}

If only 1 field out of 4 is available

I am using the below code on a WP site.
<?php
$images = array();
$images[] = $profile_user->banner_image_1;
$images[] = $profile_user->banner_image_2;
$images[] = $profile_user->banner_image_3;
$images[] = $profile_user->banner_image_4;
if(!empty($images[0]) || !empty($images[1])|| !empty($images[2])|| !empty($images[3])){
?>
<?php echo '<div class="slider2">'; ?>
<?php foreach($images as $img): ?>
<?php if(!empty($img)): ?>
<div>
<img src="
<?php
$image_id = $img;
$post_image_data = wp_get_attachment_image_src( $image_id, $size='profile_banner_img' );
echo $post_image_data[0];
?>" />
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php echo '</div>'; ?>
<?php } ?>
What i need to do is use a "IF" or "array" statement or something that will see if only "banner_image_1" has been filled by user and display a different code.
In other words if banner_image_1 returns information but 2,3,4 dont i need remove the
<div class="slider2"> and the </div>
Try this:
<?php
$images = array();
for($i=1; $i<=4; $i++) {
if(!empty($profile_user->{"banner_image_$i"})) {
$images[] = $profile_user->{"banner_image_$i"};
}
}
$validPics = count($images);
if($validPics > 0) {
if($validPics > 1) echo '<div class="slider2">';
foreach($images as $img_id) {
$img_src = wp_get_attachment_image_src( $img_id, $size='profile_banner_img' );
echo '<div><img src=\"' . $img_src[0] . '" /></div>';
}
if($validPics > 1) echo '</div>';
}

Convert current animated banner into a nivo slider from bespoke CMS

I've taken over a website that currently has a custom built CMS which we want to keep. The one change we want to make is to the homepage animated banner as it's pretty poor.
It's currently setup so it grabs the images input through the cms and outputs them in an animated banner. I want to keep that functionality but feed the images into a nivo slider instead. I'm a little cautious as to how I go about doing it.
This is the code that's outputting the images into the animated banner (I think!!):
<? if ($page[id] == 1) { ?>
<?
$i = 0;
$homebanners = mysql_query("SELECT * FROM banners ORDER BY banner_order ASC");
while ($banner = mysql_fetch_assoc($homebanners)) {
if (!$first) { $first = true; $bannerimage = $banner[banner_image]; $bannertext = $banner[banner_text]; $bannerlink = $banner[banner_link]; }
$javascript .= "bannerimage[$i] = '$banner[banner_image]';
bannertext[$i] = '".addslashes($banner[banner_text])."';
bannerlink[$i] = '$banner[banner_link]';
";
$i++;
}
if ($i > 1) {
?>
<script>
var curbanner = 0;
var bannerimage = new Array();
var bannertext = new Array();
var bannerlink = new Array();
<? echo $javascript; ?>
totalbanners = bannerimage.length;
function changebanner() {
curbanner = curbanner + 1;
if (totalbanners == curbanner) { curbanner = 0; }
bannerurl = 'banner_images/'+bannerimage[curbanner];
$('#bannertext').fadeOut('100', function() {
$("#banner").animate({"height": "0px"}, 350, "linear",
function() {
$('#banner').css({ 'background-image': 'url('+bannerurl+')' }).fadeIn('slow');
$("#banner").animate({"height": "222px"}, 350, "linear",
function() {
document.getElementById('btext').innerHTML=bannertext[curbanner];
document.getElementById('bannerlink').href=bannerlink[curbanner];
if (bannerlink[curbanner] == "") { document.getElementById('bannerlink').innerHTML = ''; } else { document.getElementById('bannerlink').innerHTML = 'Read more...'; }
$('#bannertext').fadeIn('100');
});
});
});
}
setInterval('changebanner()',10000);
</script><? } ?>
And...
<div id="rightcol" style="height:222px;">
<div id="banner" style="background-image:url('banner_images/<? echo $bannerimage; ?>')">
<div id="bannertext">
<h2 id="btext"><? echo $bannertext; ?></h2>
<a id="bannerlink" href="<? echo $bannerlink; ?>" class="readmore"><? if ($bannerlink) { ?>Read more...<? } ?></a>
</div><!-- END bannertext -->
</div><!-- END banner -->
</div><!-- END rightcol -->
How can I get the image, banner text and banner link into a nivo slider in this format:
<div id="slider" class="nivoSlider">
<img src="image-1.jpg" alt="" title="banner-text-1" />
<img src="image-2.jpg" alt="" title="banner-text-2" />
<img src="image-3.jpg" alt="" title="banner-text-3" />
</div>
The amount of slides inputted through the cms should be infinite.
<?php
if ($page[id] == 1) {
$i = 0;
$html = '';
$homebanners = mysql_query("SELECT * FROM banners ORDER BY banner_order ASC");
while ($banner = mysql_fetch_assoc($homebanners)) {
if (!$first) { $first = true; $bannerimage = $banner[banner_image]; $bannertext = $banner[banner_text]; $bannerlink = $banner[banner_link]; }
$html .= "<img src=\"$bannerimage\" alt=\"$bannertext\" title=\"$bannertext\" />";
$i++;
}
if ($i > 1) {
echo "<div id=\"slider\" class=\"nivoSlider\">";
echo $html;
echo "</div>";
}
}
?>

Categories