php - What is the best way to display a feed of different media types? -


i've created php file named feed.php designed fetch items mysql database , display them (in news feed style way) on webpage. way i've done involves using 19 if/elseif/else statements - , number growing.

in short, these statements decide how information displayed. i'll include code below, quite long , repetitive.

<?  $con = mysqli_connect("localhost","username","password","database");  if (!$con) {   die('could not connect: ' . mysqli_connect_error()); }   $query = "select * mainfeed order timestamp desc limit 0 , 1000";  $comments = mysqli_query($con, $query);  echo "<h1>recent posts</h1><br><br><hr>";  while($row = mysqli_fetch_array($comments, mysqli_assoc)) {   $comment = $row['comment'];   $timestamp = $row['timestamp'];   $file = $row['file'];   $info = pathinfo($file);   $info['extension'];   $comment = htmlspecialchars($row['comment'],ent_quotes);   $ytlink = $row['ytlink'];   $tag = $row['tag'];   $tag2 = $row['tag2'];   $tag3 = $row['tag3'];   $handle = $row['handle'];   $score = $row['score'];   $id = $row['id'];    $ytarray=explode("/", $ytlink);   $ytendstring=end($ytarray);   $ytendarray=explode("?v=", $ytendstring);   $ytendstring=end($ytendarray);   $ytendarray=explode("&", $ytendstring);   $ytcode=$ytendarray[0];    $display = array('jpg', 'jpeg', 'png', 'png', 'gif');   $vid = array('mov', 'mov', 'mp4');   if($tag != ''){     $tagdisp = "<br><a href='/$tag' class='posttag'>$tag</a>";  }  elseif($tag==''){     $tagdisp="";  }  if($tag2 != ''){     $tagdisp2 = "<a href='/$tag2' class='posttag'>$tag2</a>";  }  elseif($tag2==''){     $tagdisp2="";  }  if($tag3 != ''){     $tagdisp3 = "<a href='/$tag3' class='posttag'>$tag3</a>";  }  elseif($tag3==''){     $tagdisp3="";  }  if($tag != 'explicit' && $tag2 != 'explicit' && $tag3 != 'explicit'){  if ($file != null) {    if (in_array($info['extension'], $display)){    echo " <div class='card'>       <h3>handle: $handle</h3><br />       <p>$comment</p><br /><br />       <a href='$file'><img src='$file' style='width:70%; margin-left:15%; margin-right:15%; margin-top:5px; border-style:none;'></a><br>       <p>post #$id</p>       <p id='scorecount-$id'>score: $score</p><br>       <button onclick='myfunction($id,1)'>&#8679;</button><button onclick='myfunction($id,-1)'>&#8681;</button><br>       <br>$tagdisp $tagdisp2 $tagdisp3<br>       <p style='color: grey'>$timestamp</p><hr>     </div>   ";   }    elseif(in_array($info['extension'], $vid)) {     echo " <div class='card'>     <h3>handle: $handle</h3><br />     <p>$comment</p><br /><br />      <video controls='controls' width='70%' src='$file' style='border-style: solid'></video><br>     <p>post #$id</p>       <p id='scorecount-$id'>score: $score</p><br>       <button onclick='myfunction($id,1)'>&#8679;</button><button onclick='myfunction($id,-1)'>&#8681;</button><br>       <br>$tagdisp $tagdisp2 $tagdisp3<br>     <p style='color: grey'>$timestamp</p><hr>     </div>    "; }    else {    echo " <div class='card'>       <h3>handle: $handle</h3><br />       <p>$comment</p><br /><br />       <a href='$file'><h3>click here view attached file</h3></a><br>       <p>post #$id</p>       <p id='scorecount-$id'>score: $score</p><br>       <button onclick='myfunction($id,1)'>&#8679;</button><button onclick='myfunction($id,-1)'>&#8681;</button><br>       <br>$tagdisp $tagdisp2 $tagdisp3<br>       <p style='color: grey'>$timestamp</p><hr>     </div>   ";    } }  elseif($ytlink != null) {    echo " <div class='card'>       <h3>handle: $handle</h3><br />       <p>$comment</p><br /><br />       <iframe width=\"420\" height=\"315\" src=\"http://www.youtube.com/embed/$ytcode\" frameborder=\"0\" allowfullscreen></iframe><br />       <p>post #$id</p>       <p id='scorecount-$id'>score: $score</p><br>       <button onclick='myfunction($id,1)'>&#8679;</button><button onclick='myfunction($id,-1)'>&#8681;</button><br>       <br>$tagdisp $tagdisp2 $tagdisp3<br>       <p style='color: grey'>$timestamp</p><hr>     </div>   ";  }  else {    echo " <div class='card'>       <h3>handle: $handle</h3><br />       <p>$comment</p><br />       <p>post #$id</p>       <p id='scorecount-$id'>score: $score</p><br>       <button onclick='myfunction($id,1)'>&#8679;</button><button onclick='myfunction($id,-1)'>&#8681;</button><br>       <br>$tagdisp $tagdisp2 $tagdisp3<br>       <p style='color: grey'>$timestamp</p><hr>     </div>   ";  }  }  if($tag == 'explicit' or $tag2 == 'explicit' or $tag3 == 'explicit'){  if ($file != null) {    if (in_array($info['extension'], $display)){    echo " <div class='cardexp'>       <h3>handle: $handle</h3><br />       <p>$comment</p><br /><br />       <img src='$file' style='width:70%; margin-left:15%; margin-right:15%; margin-top:5px; border-style:solid;'><br>       <p>post #$id</p>       <p id='scorecount-$id'>score: $score</p><br>       <button onclick='myfunction($id,1)'>&#8679;</button><button onclick='myfunction($id,-1)'>&#8681;</button><br>       <br>$tagdisp $tagdisp2 $tagdisp3<br>       <p style='color: grey'>$timestamp</p><hr>     </div>   ";   }    elseif(in_array($info['extension'], $vid)) {     echo " <div class='cardexp'>     <h3>handle: $handle</h3><br />     <p>$comment</p><br /><br />      <video controls='controls' width='70%' src='$file' style='border-style: solid'></video><br>     <p>post #$id</p>       <p id='scorecount-$id'>score: $score</p><br>       <button onclick='myfunction($id,1)'>&#8679;</button><button onclick='myfunction($id,-1)'>&#8681;</button><br>       <br>$tagdisp $tagdisp2 $tagdisp3<br>     <p style='color: grey'>$timestamp</p><hr>     </div>    "; }    else {    echo " <div class='cardexp'>       <h3>handle: $handle</h3><br />       <p>$comment</p><br /><br />       <a href='$file'><h3>click here view attached file</h3></a><br>       <p>post #$id</p>       <p id='scorecount-$id'>score: $score</p><br>       <button onclick='myfunction($id,1)'>&#8679;</button><button onclick='myfunction($id,-1)'>&#8681;</button><br>       <br>$tagdisp $tagdisp2 $tagdisp3<br>       <p style='color: grey'>$timestamp</p><hr>     </div>   ";    } }  elseif($ytlink != null) {    echo " <div class='cardexp'>       <h3>handle: $handle</h3><br />       <p>$comment</p><br /><br />       <iframe width=\"420\" height=\"315\" src=\"http://www.youtube.com/embed/$ytcode\" frameborder=\"0\" allowfullscreen></iframe><br />       <p>post #$id</p>       <p id='scorecount-$id'>score: $score</p><br>       <button onclick='myfunction($id,1)'>&#8679;</button><button onclick='myfunction($id,-1)'>&#8681;</button><br>       <br>$tagdisp $tagdisp2 $tagdisp3<br>       <p style='color: grey'>$timestamp</p><hr>     </div>   ";  }   else {    echo " <div class='cardexp'>       <h3>handle: $handle</h3><br />       <p>$comment</p><br />       <p>post #$id</p>       <p id='scorecount-$id'>score: $score</p><br>       <button onclick='myfunction($id,1)'>&#8679;</button><button onclick='myfunction($id,-1)'>&#8681;</button><br>       <br>$tagdisp $tagdisp2 $tagdisp3<br>       <p style='color: grey'>$timestamp</p><hr>     </div>   ";  }  }  }  mysqli_close($con);  ?> 

i feel method i've used create feed (and way i've told browser display different media types, not best way in have done it.

if rewrite file, best (most efficient , effective) way of achieving same result?

many thanks.

function getcardtemplate($comment, $id, $score, $tagdisp, $tagdisp2, $tagdisp3, $timestamp, $innertemplate) {   return " <div class='card'>       <h3>handle: $handle</h3><br />       <p>$comment</p><br /><br />".$innertemplate.       "<p>post #$id</p>       <p id='scorecount-$id'>score: $score</p><br>       <button onclick='myfunction($id,1)'>&#8679;</button><button onclick='myfunction($id,-1)'>&#8681;</button><br>       <br>$tagdisp $tagdisp2 $tagdisp3<br>       <p style='color: grey'>$timestamp</p><hr>     </div>   "; } 

and pass text needed $innertemplate. still have many cases, less repetitive , more readable , easier maintain.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -