Jump to content

If and Else If - is there a better way?


y2yang

Recommended Posts

Hello people!

I have been messing around with this code for about 2 months now and I have just decided, there has to be a better way, a shorter way.

 

The problem is I have over 100 Elseif statements, and they're not just a few lines, but many lines in just one elseif itself. All the elseif contains similar code, like table structure and such with just a few changes in contents, like pictures and words.

 

Is there a better script for me to achieve something similar to the 'If' and 'Elseif' statement? Maybe using Javascript if I have to.

 

Here is the code for reference:

 

<?php

if(!$Pid){ $Pid = $HTTP_GET_VARS['Pid']; }

if($Pid=="1"){
?>

<table width="100%">
<colgroup>
	<col width="55px" /><col width="525" /><!--<col width="405" /><col width="55px" /><col width="65px" />-->

</colgroup>
<tr><td colspan="4"><div class="BLine2"></div></td>
</tr>
<tr style="height:28px;">
	<td class="vam tac"><img src="http://image.vickizhao.net/style_guide/icn/icn_starhome_title.gif" alt="" /></td>
	<td class="vam tal"><span style="margin:0 0 0 5px;">Movies <Hua Mu Lan> shooting scene 2009.02</span></td>
	<!--
	<td class="vam tac"><img src="http://image.vickizhao.net/style_guide/icn/icn_starhome_date.gif" alt="" /></td>
	<td class="vam tal"><span>2008-12-09</span></td>
	-->
</tr>

<tr><td colspan="4"><div class="line2"></div></td></tr>
<!-- photo description  -->
<tr>
	<td colspan="4" class="tac">
		<div id="strContent" style="margin:20px 0 20px 0;width:580px;overflow:auto;">
			<img src="http://file.vickizhao.net/files/2009/0225/zhaowei_mulan_001.jpg" /><br />
			<div class="lh20" style="margin:10px 0 0 0;"><P>Movies <Hua Mu Lan> shooting scene 2009.02</P></div>

		</div>
		<script type="text/javascript">reSizeImgContent('strContent',580);</script>
	</td>
</tr>
<tr><td colspan="4"><div class="BLine2"></div></td></tr>
<tr style="height:35px;" class="tar">
	<td colspan="4"><a href="../list.php?Pid=1"><img src="http://image.vickizhao.net/style_guide/btn/btn_starhome_list.gif" class="hand" alt="" /></a></td>
</tr>
</table>

<!-- start comments -->

<?php require("comment/ShowHideComments.php"); ?>

<div id="viewComment" style="display:block;width:580px;">
   <iframe src="comment/photo_1.php" frameborder="0" width="580" height="100%" scrolling="no" name="ifrmComment" id="ifrmComment"></iframe>
</div>
<!-- end comments -->

<p style="margin:40px;"></p>

<!-- begin prev / next -->
<table width="100%">
<colgroup>
	<col width="70" /><col />
</colgroup>
<tr style="height:2px;"><td colspan="2" class="bgc9"></td></tr>
    
<tr style="height:25px;">
	<td><img src="http://image.vickizhao.net/style_guide/icn/icn_starhome_prev.gif" class="vam" alt="" /></td>
	<td class="tal fc14"><span style="margin:0 0 0 5px;"><a href="read.php?Pid=2">Movies <Hua Mu Lan> shooting scene 2009.02</a></span></td>
</tr>
    
<tr style="height:2px;"><td colspan="2" class="bgc9"></td></tr>
</table>

<p style="margin:40px;"></p>

<div class="tal" style="margin:0 0 3px 0;"><img src="http://image.vickizhao.net/starhome/photo/write/txt_photolist.gif" class="vam" alt="" /></div>
<div class="line2"></div>


<p style="margin:22px;"></p>

<!-- photo list -->

<?php require("../PList/1.php"); ?>

<?php
}
elseif($Pid=="2"){
?>

<table width="100%">
<colgroup>
	<col width="55px" /><col width="525" /><!--<col width="405" /><col width="55px" /><col width="65px" />-->

</colgroup>
<tr><td colspan="4"><div class="BLine2"></div></td>
</tr>
<tr style="height:28px;">
	<td class="vam tac"><img src="http://image.vickizhao.net/style_guide/icn/icn_starhome_title.gif" alt="" /></td>
	<td class="vam tal"><span style="margin:0 0 0 5px;">Movies <Hua Mu Lan> shooting scene 2009.02</span></td>
	<!--
	<td class="vam tac"><img src="http://image.vickizhao.net/style_guide/icn/icn_starhome_date.gif" alt="" /></td>
	<td class="vam tal"><span>2008-12-09</span></td>
	-->
</tr>

<tr><td colspan="4"><div class="line2"></div></td></tr>
<!-- photo description  -->
<tr>
	<td colspan="4" class="tac">
		<div id="strContent" style="margin:20px 0 20px 0;width:580px;overflow:auto;">
			<img src="http://file.vickizhao.net/files/2009/0225/zhaowei_mulan_002.jpg" /><br />
			<div class="lh20" style="margin:10px 0 0 0;"><P>Movies <Hua Mu Lan> shooting scene 2009.02</P></div>

		</div>
		<script type="text/javascript">reSizeImgContent('strContent',580);</script>
	</td>
</tr>
<tr><td colspan="4"><div class="BLine2"></div></td></tr>
<tr style="height:35px;" class="tar">
	<td colspan="4"><a href="../list.php?Pid=1"><img src="http://image.vickizhao.net/style_guide/btn/btn_starhome_list.gif" class="hand" alt="" /></a></td>
</tr>
</table>

<!-- start comments -->

<?php require("comment/ShowHideComments.php"); ?>

<div id="viewComment" style="display:block;width:580px;">
   <iframe src="comment/photo_2.php" frameborder="0" width="580" height="100%" scrolling="no" name="ifrmComment" id="ifrmComment"></iframe>
</div>
<!-- end comments -->

<p style="margin:40px;"></p>

<!-- begin prev / next -->
<table width="100%">
<colgroup>
	<col width="70" /><col />
</colgroup>
<tr style="height:2px;"><td colspan="2" class="bgc9"></td></tr>
<tr style="height:25px;">
	<td><img src="http://image.vickizhao.net/style_guide/icn/icn_starhome_prev.gif" class="vam" alt="" /></td>
	<td class="tal fc14"><span style="margin:0 0 0 5px;"><a href="read.php?Pid=3">Movies <Hua Mu Lan> shooting scene 2009.02</a></span></td>
</tr>
<tr style="height:1px;"><td colspan="2" class="bgc9"></td></tr>
<tr style="height:25px;">
	<td><img src="http://image.vickizhao.net/style_guide/icn/icn_starhome_next.gif" class="vam" alt="" /></td>
	<td class="tal fc14"><span style="margin:0 0 0 5px;"><a href="read.php?Pid=1">Movies <Hua Mu Lan> shooting scene 2009.02</a></span></td>
</tr>
<tr style="height:2px;"><td colspan="2" class="bgc9"></td></tr>
</table>

<p style="margin:40px;"></p>

<div class="tal" style="margin:0 0 3px 0;"><img src="http://image.vickizhao.net/starhome/photo/write/txt_photolist.gif" class="vam" alt="" /></div>
<div class="line2"></div>


<p style="margin:22px;"></p>

<!-- photo list -->

<?php require("../PList/1.php"); ?>

<?php
}
elseif($Pid=="3"){
?>

<table width="100%">
<colgroup>
	<col width="55px" /><col width="525" /><!--<col width="405" /><col width="55px" /><col width="65px" />-->

</colgroup>
<tr><td colspan="4"><div class="BLine2"></div></td>
</tr>
<tr style="height:28px;">
	<td class="vam tac"><img src="http://image.vickizhao.net/style_guide/icn/icn_starhome_title.gif" alt="" /></td>
	<td class="vam tal"><span style="margin:0 0 0 5px;">Movies <Hua Mu Lan> shooting scene 2009.02</span></td>
	<!--
	<td class="vam tac"><img src="http://image.vickizhao.net/style_guide/icn/icn_starhome_date.gif" alt="" /></td>
	<td class="vam tal"><span>2008-12-09</span></td>
	-->
</tr>

<tr><td colspan="4"><div class="line2"></div></td></tr>
<!-- photo description  -->
<tr>
	<td colspan="4" class="tac">
		<div id="strContent" style="margin:20px 0 20px 0;width:580px;overflow:auto;">
			<img src="http://file.vickizhao.net/files/2009/0225/zhaowei_mulan_003.jpg" /><br />
			<div class="lh20" style="margin:10px 0 0 0;"><P>Movies <Hua Mu Lan> shooting scene 2009.02</P></div>

		</div>
		<script type="text/javascript">reSizeImgContent('strContent',580);</script>
	</td>
</tr>
<tr><td colspan="4"><div class="BLine2"></div></td></tr>
<tr style="height:35px;" class="tar">
	<td colspan="4"><a href="../list.php?Pid=1"><img src="http://image.vickizhao.net/style_guide/btn/btn_starhome_list.gif" class="hand" alt="" /></a></td>
</tr>
</table>

<!-- start comments -->

<?php require("comment/ShowHideComments.php"); ?>

<div id="viewComment" style="display:block;width:580px;">
   <iframe src="comment/photo_3.php" frameborder="0" width="580" height="100%" scrolling="no" name="ifrmComment" id="ifrmComment"></iframe>
</div>
<!-- end comments -->

<p style="margin:40px;"></p>

<!-- begin prev / next -->
<table width="100%">
<colgroup>
	<col width="70" /><col />
</colgroup>
<tr style="height:2px;"><td colspan="2" class="bgc9"></td></tr>
<tr style="height:25px;">
	<td><img src="http://image.vickizhao.net/style_guide/icn/icn_starhome_prev.gif" class="vam" alt="" /></td>
	<td class="tal fc14"><span style="margin:0 0 0 5px;"><a href="read.php?Pid=4">Movies <Hua Mu Lan> Happy Birthday Zhao Wei 2009.03</a></span></td>
</tr>
<tr style="height:1px;"><td colspan="2" class="bgc9"></td></tr>
<tr style="height:25px;">
	<td><img src="http://image.vickizhao.net/style_guide/icn/icn_starhome_next.gif" class="vam" alt="" /></td>
	<td class="tal fc14"><span style="margin:0 0 0 5px;"><a href="read.php?Pid=2">Movies <Hua Mu Lan> shooting scene 2009.02</a></span></td>
</tr>
<tr style="height:2px;"><td colspan="2" class="bgc9"></td></tr>
</table>

<p style="margin:40px;"></p>

<div class="tal" style="margin:0 0 3px 0;"><img src="http://image.vickizhao.net/starhome/photo/write/txt_photolist.gif" class="vam" alt="" /></div>
<div class="line2"></div>


<p style="margin:22px;"></p>

<!-- photo list -->

<?php require("../PList/1.php"); ?>

<?php
}
elseif($Pid=="4"){
?>

...and on over 100 elseif.

 

As you can see where I am heading. There's no way I can manage over 100 elseif that undergo changes quite often. Not to mention there are more directories which uses this same format. It's a pain, so please help me out a little.

 

Please ask if code look confusing or need more info.   

Link to comment
https://forums.phpfreaks.com/topic/156197-if-and-else-if-is-there-a-better-way/
Share on other sites

You can use switch, yes. Another thing is if you look over all your HTML in each if statement, you'll see that there are a lot of parts that are the same. You can just pull those out of the if statements and just use the if statements on parts that are different. That should reduce down the size of the file and repetition.

Even with a switch/case statement, you should only use it if you have at most 10 choices that result in TOTALLY different output. For similar output, where only the information in it, like a link or text changes, you should be using variables in ONE common block of HTML.

 

I did not look through the code to determine what was different or the same between the different choices, but for anything more than about 10 different choices, you should be using a database. If the output is TOTALLY different for each choice, the whole content for each choice would be stored as an entry in the database. If only the information in ONE common block of HTML is different for each choice, only that information would be stored as entries in the database.

 

 

Would something like this work?

 

Instead of having to repeat all of the table structure,

 

Would it be better to do this instead:

<td class="vam tal"><span style="margin:0 0 0 5px;"><?php require("...page90Title.php"); ?></span></td>

<td colspan="4" class="tac">
		<div id="strContent" style="margin:20px 0 20px 0;width:580px;overflow:auto;">
			<?php require("...page90Image.php"); ?><br />
			<div class="lh20" style="margin:10px 0 0 0;"><P><?php require("...page90Content.php"); ?></P></div>

		</div>
		<script type="text/javascript">reSizeImgContent('strContent',580);</script>
	</td>

 

and so on with any other contents that are different on different page?

 

What I did was having all the contents that are different from page to page to have a php required call for content corresponded to that page. But the problem is I'll now have like 5 to 10 or so php call in one page with over thousands of called php pages. 1 page = 5~10 called pages.

 

Or am I just making this harder than it seen?

Even with a switch/case statement, you should only use it if you have at most 10 choices that result in TOTALLY different output. For similar output, where only the information in it, like a link or text changes, you should be using variables in ONE common block of HTML.

 

I did not look through the code to determine what was different or the same between the different choices, but for anything more than about 10 different choices, you should be using a database. If the output is TOTALLY different for each choice, the whole content for each choice would be stored as an entry in the database. If only the information in ONE common block of HTML is different for each choice, only that information would be stored as entries in the database.

 

Yes only the link and text/images changes from page to page, all other like structure and format are the same.

How would I go about storing those into database? Preferably MySQL since I have only been using MySQL to store data. Tho I only understand a fair amount about MySQL. No technical stuff.

I did compare the first two blocks (using winmerge http://winmerge.org/) and the only difference is an <img src="...." file name, an <iframe src="..." file name, and believe it or not, hard coded pagination (previous/next links.)

 

No offense if you were the one that made the code that way, but the whole point of a programming language is to use variables to hold values that change so that you only need to setup the variables with different values in order to reuse code multiple times.

 

In the end, you would need to store the two different file names that are different between each choice in a database and use a little php pagination code to replace the hard coded previous/next links.

 

I'm going to guess that the existing scheme is repeated for each different movie series? If so, the database scheme could be expanded to hold all the information and one single .php could replace all the 100's or 1000's of folders and files you have now.

Thanks PFMaBiSmAd.

 

I will surely look into inputting my variables information into database. Tho will probably take me a confused amount of time. For a head start, by any chance can you give me a sample, non-working and incomplete code is all i asked, just so I'll know what direction I am heading to.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.