Jump to content

Easiest way to repeat code without doing it all again?


Michan

Recommended Posts

Hi,

I've created some code to display the first entry entry in a chart of 10 videogames. Now, this code will stay exactly the same for the second entry, but instead of the row being 's1', it will be 's2', and so on for the rest of the chart.

But I can't find a way to produce the second entry (s2) without reproducing the entire code due to the value in the query...

Reproducing this ten times would be incredibly clunky so I was wondering if there would be an easier way? (Also, is my code for producing the dates okay? It's hand-made.)

[code]
$chart = mysql_query('SELECT * FROM vg_charts LEFT JOIN vg_games ON vg_charts.s1=vg_games.id WHERE type=1 ORDER BY date DESC limit 0, 1');

while ($ukchart = mysql_fetch_array($chart))
{

$release=$ukchart['release_date'];

$rday = substr($release, 8, 2);
$rmon = substr($release, 5, 2);
$ryea = substr($release, 0, 4);

if ($rmon == "01")
$rmon = "Jan";
elseif ($rmon == "02")
$rmon = "Feb";
elseif ($rom == "03")
$rmon = "Mar";
elseif ($rmon == "04")
$rmon = "Apr";
elseif ($rom == "05")
$rmon = "May";
elseif ($rmon == "06")
$rmon = "Jun";
elseif ($rom == "07")
$rmon = "Jul";
elseif ($rmon == "08")
$rmon = "Aug";
elseif ($rom == "09")
$rmon = "Sep";
elseif ($rmon == "10")
$rmon = "Oct";
elseif ($rom == "11")
$rmon = "Nov";
elseif ($rmon == "12")
$rmon = "Dec";

if (!$release)
$rdate = "Release date N/A";
elseif ($release=="0000-00-00 00:00:00")
$rdate = "Release date N/A";
else
$rdate = $rday." ".$rmon." ".$ryea;

echo ('

<div id="home-chart-item1">
<div id="home-chart-item1rank" style="color: #5C5C5C">1<BR><img src="gfx/chart_new.gif"></div>
<div id="home-chart-item1box"><a href="gamepage/'.$ukchart['id'].'">
<img src="'.$ukchart['image'].'" border="0" width="36" height="50">
</a>
</div>
<div id="home-chart-item1text"><a href="gamepage/'.$ukchart['id'].'">'.$ukchart['name'].'</a><BR>'.$ukchart['publisher'].'<BR><span style="color: #949494">'.$rdate.'</span>
</div>
</div>

');
}
[/code]

Many thanks in advance :)

-Mi
[quote]I've created some code to display the first entry entry in a chart of 10 videogames. Now, this code will stay exactly the same for the second entry, but instead of the row being 's1', it will be 's2'[/quote]

Can we see your tabnle structure, this doesn't sound right.
Hi, it must be something like this:

[code]
<?php
for($i=0;$i<10;$i++){
$chart=mysql_query('SELECT * FROM vg_charts LEFT JOIN vg_games ON vg_charts.s$i=vg_games.id WHERE type=1 ORDER BY date DESC limit 0, 1');
}
?>
[/code]

...the date procedure seems to be ok
Here's an easier way of getting the months.

Instead of:
[code]<?php
if ($rmon == "01")
$rmon = "Jan";
elseif ($rmon == "02")
$rmon = "Feb";
elseif ($rom == "03")
$rmon = "Mar";
elseif ($rmon == "04")
$rmon = "Apr";
elseif ($rom == "05")
$rmon = "May";
elseif ($rmon == "06")
$rmon = "Jun";
elseif ($rom == "07")
$rmon = "Jul";
elseif ($rmon == "08")
$rmon = "Aug";
elseif ($rom == "09")
$rmon = "Sep";
elseif ($rmon == "10")
$rmon = "Oct";
elseif ($rom == "11")
$rmon = "Nov";
elseif ($rmon == "12")
$rmon = "Dec";
?>[/code]
You can do:
[code]<?php
$months = array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$rmon = $months[$rmon];
?>[/code]
or
[code]<?php
$rmon = date('M',strtotime($rmon . '/1/2006'));
?>[/code]

Ken

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.