Jump to content

consolidating echos


Ixtomos

Recommended Posts

i use the following code to limit/show what people see, im trying to think of a better way to write this all out so its not so many echos, any suggestions?

 

if ($catee == $catid && $r["Rank"] <= $personrank && $type == "text")
{
  echo "<tr>";
  echo "<td style=\"padding-left: 5px; padding-right: 5px\" class=\"mainlevel\"><b>";
     echo "<label id=\"searchlink$id\" rel=\"subcontent$id\">$name</label>";
    echo "</td>";
  echo "<td class=\"downloads\"><a href='?page=downloadtest&cmd=show&id=$id&type=$type'class=\"mainlevel\">View</a></td>";
  echo "<td class=\"downloads\"><a href='download.php?cmd=download&id=$id&type=$type'class=\"mainlevel\">Download</a></td>";
  echo "<td class=\"downloads\">$addby</td>";
  echo "<td class=\"downloads\"><center>$rank</center></td>";
  if (isadmin ( $_SESSION['user_id'] ) or $uname==$addby) {
  echo "<td class=\"downloads\"><center><a href=\"?page=downloadtest&cmd=delete&id=$id\" class=\"mainlevel\">D</a></center></td>";
  echo "<td class=\"downloads\"><center><a href=\"?page=downloadtest&cmd=edit&id=$id\" class=\"mainlevel\">E</a></center></td>";
  }
  echo "</tr>";
   } elseif ($catee==$catid && $r["Rank"]>$personrank) {
  echo "<tr>";
  echo "<td style=\"padding-left: 5px; padding-right: 5px\" class=\"mainlevel\"><b>";
     echo "<label >$name</label>";
    echo "</td>";
    echo "<td class=\"downloads\">Can not view</td>";
  echo "<td class=\"downloads\">Can not download</td>";
  echo "<td class=\"downloads\">$addby</td>";
  echo "<td class=\"downloads\"><center>$rank</center></td>";
if (isadmin ( $_SESSION['user_id'] ) or $uname==$addby) {
  echo "<td class=\"downloads\"><center><a href=\"?page=downloadtest&cmd=delete&id=$id\" class=\"mainlevel\">Y</a></center></td>";
  }
  echo "</tr>";
} elseif ($catee==$catid && $type=="image") {
  echo "<tr>";
  echo "<td style=\"padding-left: 5px; padding-right: 5px\" class=\"mainlevel\"><b>";
     echo "<label id=\"searchlink$id\" rel=\"subcontent$id\">$name</label>";
  echo "<DIV id=\"subcontent$id\" class=\"mainlevel2\" style=\"position:absolute; visibility: hidden;border: 3px solid white; width: 500px; padding: 8px;\">";
  echo "$desc";
  echo "<br>";
  echo "<a  style=\"color:#CCC\" href=\"javascript:dropdowncontent.hidediv('subcontent$id')\" class=\"mainlevel2\">Hide this DIV manually</a>";
  echo "</DIV>";
    echo "</td>";
    echo "<td class=\"downloads\"><a href='' onclick=\"return popitup('download.php?cmd=show&id=$id&type=$type')\"class=\"mainlevel\">View</a></td>";
  echo "<td class=\"downloads\"><a href='download.php?cmd=download&id=$id&type=$type'class=\"mainlevel\">Download</a></td>";
  echo "<td class=\"downloads\">$addby</td>";
  echo "<td class=\"downloads\"><center>$rank</center></td>";
if (isadmin ( $_SESSION['user_id'] ) or $uname==$addby) {
  echo "<td class=\"downloads\"><center><a href=\"?page=downloadtest&cmd=delete&id=$id\" class=\"mainlevel\">Y</a></center></td>";
  }
  echo "</tr>";
  echo "<script type=\"text/javascript\">";
  echo "dropdowncontent.init(\"searchlink$id\", \"right-bottom\", 500, 'mouseover')";
  echo "</script>";
}
   }

 

if i forgot anything i am sorry,

 

and thanks in advance for help

Link to comment
Share on other sites

Is there something specific about the echos that bothers you?

 

If you don't like having to escape the double quotes, you could change some of the lines to single quotes. Just be aware that variables (and special characters like "\n") won't work inside of single-quoted strings.

 

<?php

echo '<tr>';
echo '<td style="padding-left: 5px; padding-right: 5px" class="mainlevel"><b>';
echo '<label id="searchlink' . $id . '" rel="subcontent' . $id . '">' . $name . '</label>';
echo '</td>';
?>

 

 

You could also consider jumping in and out of PHP. Of course, this can get a little messy too.

 

<tr>
<td style="padding-left: 5px; padding-right: 5px" class="mainlevel"><b>
<label id="searchlink<?php echo $id; ?>" rel="subcontent<?php echo $id; ?>"><?php echo $name; ?></label>
</td>

Link to comment
Share on other sites

This might a prime candidate for a template engine, whether it be a really simple one you make yourself or a already established one like Twig or Smarty. Though, unless you're going to use the template engine for the entire site (which I would recommend you to at least consider), then the big and complex template engines might be a bit overkill.

At the very least you should be moving all of that code into a function of its own. Have it construct the menu as as string, and then return the constructed string to the calling function. Then you can echo it out wherever you need it to be.

 

Also, I notice that your HTML code is very messy, with lots of deprecated tags, mixing of structure and style, inconsistent use of event triggers (href="Javascript:" in some situations and onclick in others), and general inconsistencies within itself. So clearing up the HTML code should give you a lot of impact, in the cleanliness of the code.

This can be done with moving all the JS event handlers out of the HTML code itself, and use jQuery (or similar libraries) to attach them when the DOM is ready. Moving all of the CSS definitions into an external stylesheet, and use only classes, elements and IDs to style your page.

Also cut down your HTML code the the absolute minimum necessary, to provide you with the desired structure of your content. HTML is for giving content structure and contextual meaning, not for styling it for human presentation. Reason I mention this is because it seems like you're using a table, and some other elements, to provide a styling framework for your site. Instead of just using unordered lists, nested if need be, to provide only structure to your links.

 

I also get the impression that you're trying to do too much in that code, but that's hard to say with any degree of certainty as I don't really know what your goal is. What that code produces in the end, in other words. That said, it might be worthwhile for you to consider that, and see if you can see any logic inside of that script that could be logically separated out on its own.

Link to comment
Share on other sites

Thank you for the info,

 

the code im using right now i wrote about 2-3 years ago so i thought it was time to update. as for the styling my stylesheet does not affect the code (nor for the fact neither does anything on another page, have not figured out why yet, but will eventually fix) as for one of the big name templates, i actually prefer using my own code, i just dont like using others code but i may eventually. as for what my code does, it just shows information from my SQL database when it should be shown, (if the person has correct access), but again thank you for your information, ill take it to heart and try to figure out something i can do

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.