I've picked up a project from another developer and I'm trying to finish and clean up a bit. This part of the开发者_高级运维 code renders products from a database after checking for a language cookie:
<?php if (get_cookie('spanish')) : ?>
<?php if ($product['details_spanish'] != '') : ?>
<?php $details = explode(':',$product['details_spanish']); ?>
<h3>Especificaciones</h3>
<?php else : ?>
<?php if ($product['details'] != '') : ?>
<?php $details = explode(':',$product['details']); ?>
<h3>Especificaciones</h3>
<?php endif ?>
<?php endif; ?>
<?php else : ?>
<?php if ($product['details'] != '') : ?>
<?php $details = explode(':',$product['details']); ?>
<h3>Specifications</h3>
<?php endif; ?>
<?php endif; ?>
<ul>
<?php if ($details) : ?>
<?php foreach ($details as $detail) : ?>
<?php $detail = split(',',$detail); ?>
<?php if ($detail[0] != '' && $detail[1] != '') : ?>
<li>
<strong><?=ucwords($detail[0])?></strong> : <?=$detail[1]?>
</li>
<?php endif; ?>
<?php $i++; ?>
<?php endforeach; ?>
<?php endif; ?>
Sorry, I know this is hard to read due to inconsistency and bad coding (not my project initially. What it's doing is checking for a spanish cookie on the users machine. If it finds it, it pulls the values from the spanish column of the table instead of the regular. It falls back on the english value if there is no spanish value in the table. Also, it creates a $details
variable that contains one of the values of the database, and splits it where the :
is.
The problem I'm having is on some products, it gives me an error saying it can't find the $details variable, I'm assuming it is because the variable doesn't live outside the scope of the first if
statement?
I'm a php noob really but even I know this isn't good practice. is there a better way to clean this up and have my $detail variable available?
Quick workaround is to pre-initialize that variable at the very top:
<?php $details = ''; ?>
精彩评论