Forum Moderators: coopster

Message Too Old, No Replies

Trying to add random text to an existing php page

Struggling with something basic!

         

Spook

5:20 pm on Apr 7, 2007 (gmt 0)

10+ Year Member



Hi

I last wore my PHP hat about three years ago, and have [apparently] forgotten something very basic.

I have a simple table [1 column x 3 rows]used within an Open Source shopping cart application. The first cell is used as a heading and until now had the static text - "Special Offers". I am simply trying to replace this static text with randomly generated text drawn from a list. I have the code that produces the text but have fallen at the last hurdle - printing the output to the web page.

Random code generator:


$random_text = array("Special Offer...",
"Hot Offer...",
"Not to be Missed!",
"Order Today!",
"Special Prices...");
srand(time());
$sizeof = count($random_text);
$random = (rand()%$sizeof);

The part I am having trouble with is as follows [line 102]:

<tr><td height="26" bgcolor="#FF0000" ><h2 class="specialsHeading"> 'print("$random_text[$random]"); ' </h2></td></tr>

And the error is:

Parse error: syntax error, unexpected T_PRINT in /blah/blah/blah/blah/blah/blah/specials_index.php on line 102

I have tried, and continue to try everything I can to solve this, but if anyone can point me in the right direction it would be much appreciated.

I should probably say that this is part of a PHP generated page and if I replace the 'print("$random_text[$random]"); ' with the original "Special Offers" text, all displays correctly.

Many thanks!

[edited by: Spook at 5:22 pm (utc) on April 7, 2007]

derek mcgilvray

5:48 pm on Apr 7, 2007 (gmt 0)

10+ Year Member



After first glance I would ask if you have declared php - i.e. <?php before your intended print statement. And then close php afterwards.

Too obvious?

Spook

6:43 pm on Apr 7, 2007 (gmt 0)

10+ Year Member



Hey nothing is too obvious! :)

The entire page is PHP generated so there are opening and closing tags at the beginning and end of the page. All worked fine until I removed the text from the table cell and replaced it with the print statement. I have added the code which generates the random text at the top of the page, but moving it into the cell along with the print statement still generates an error.

Little_G

7:21 pm on Apr 7, 2007 (gmt 0)

10+ Year Member



Hi,

If this bit:

<tr><td height="26" bgcolor="#FF0000" ><h2 class="specialsHeading"> 'print("$random_text[$random]"); ' </h2></td></tr>
is being echoed then it should be:
echo '<tr><td height="26" bgcolor="#FF0000" ><h2 class="specialsHeading"> ', $random_text[$random], ' </h2></td></tr>';

if its outside a PHP block then:
<tr><td height="26" bgcolor="#FF0000" ><h2 class="specialsHeading"><?php echo $random_text[$random];?></h2></td></tr>

Also to optimise your random selector try:

$random_text = array("Special Offer...",
"Hot Offer...",
"Not to be Missed!",
"Order Today!",
"Special Prices...");
$random=mt_rand(0,count($random_text)-1);

Andrew

[edited by: Little_G at 7:22 pm (utc) on April 7, 2007]

Spook

6:19 am on Apr 8, 2007 (gmt 0)

10+ Year Member



Hi Andrew, thanks for your help.

Its inside a PHP block.

The entire section of code looks like this:


$list_box_contents[$row][$col] = array('params' => 'class="centerBoxContentsNew centeredContent back"' . ' ' . 'style="width:' . $col_width_div . '%;"',
'text' => (($specials_index->fields['products_image'] == '' and PRODUCTS_IMAGE_NO_IMAGE_STATUS == 0)? '' : '<table class="specialsBorder" cellpadding="1" cellspacing="0" border="0" width="100%" ><tr><td height="26" bgcolor="#FF0000" ><h2 class="specialsHeading">'print("$random_text[$random]");'</h2></td></tr>
<tr class="specialsStyle"><td valign="top" align="center"><a href="' . zen_href_link(zen_get_info_page($specials_index->fields['products_id']), 'products_id=' . $specials_index->fields['products_id']) . '">' . zen_image(DIR_WS_IMAGES . $specials_index->fields['products_image'], $specials_index->fields['products_name'], IMAGE_PRODUCT_NEW_WIDTH, IMAGE_PRODUCT_NEW_HEIGHT) . '</a></td></tr>') . '<tr><td valign="top" align="center"><a href="' . zen_href_link(zen_get_info_page($specials_index->fields['products_id']), 'products_id=' . $specials_index->fields['products_id']) . '">' . $specials_index->fields['products_name'] . '</a><br />' . $products_price . '</td></tr></table>');

I thought sleeping on it may help clear the fog, but no!

Thanks for the optimised code btw, I have changed it over already.

Regards

derek mcgilvray

10:26 am on Apr 8, 2007 (gmt 0)

10+ Year Member



Have you escaped your quotation marks with backslashes?

So that:
<tr><td height="26" bgcolor="#FF0000" ><h2 class="specialsHeading"><?php echo $random_text[$random];?></h2></td></tr>

becomes:
<tr><td height=\"26\" bgcolor=\"#FF0000\" ><h2 class=\"specialsHeading\"><?php echo $random_text[$random];?></h2></td></tr>

Spook

10:48 am on Apr 8, 2007 (gmt 0)

10+ Year Member



Hi Derek

No I haven't, but the table displays OK when I have as the heading

...<tr><td height="26" bgcolor="#FF0000" ><h2 class="specialsHeading"> Secial Offer </h2></td></tr>

its only when I try to insert the randomly generated text between the h2 tags that I run into trouble and I get the error messages.

I have now put the random text generation in another file and call it at the top of the page - if I echo it to the top of the page there is an output, so its there, I just cant get it to show up in the right place.

I'm guessing that there is something else I should be telling you, but can't think for the life of me what it is!

Regards

henry0

11:25 am on Apr 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All those carts are based on the same model
are you sure that the total output is governed by that page?
did you check if you need modif any .tpl or/and "languages"

Little_G

12:08 pm on Apr 8, 2007 (gmt 0)

10+ Year Member



Hi,

Right ok, this:

<h2 class="specialsHeading">'print("$random_text[$random]");'</h2>

should be this:
<h2 class="specialsHeading">' . $random_text[$random] . '</h2>

Andrew

Spook

12:27 pm on Apr 8, 2007 (gmt 0)

10+ Year Member



Hi Andrew, that's fixed it!

I knew it had to be something simple, surprised myself just how much I have forgotten. - Thanks everyone for your help.

Unfortunately I still have more work to do. There could be several of these text headings on a single page, and i would like each heading to be different, at the moment the headings change when the page is refreshed but they all read the same.

I'm guessing I have to put each 'potential' heading into an array, then loop through the array each time a heading is called?

Am I on the right track?

TIA

henry0

12:40 pm on Apr 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ARRAY SHUFFLE [us.php.net]

Spook

1:29 pm on Apr 8, 2007 (gmt 0)

10+ Year Member



That looks interesting henry0, I will give that a go and see where I finish up!

Many thanks.