Jump to content

script only displays last item in a selection


niche

Recommended Posts

I have a script that supposed to display the barcode for each item in the selection (66 rows in this case). However, it displays the barcode from the last item 66 times. How do I display each item selected? I know all the result of the mysql selection is complete.Here's the script (postnet.php

<?phprequire('class/BCGFont.php');require('class/BCGColor.php');require('class/BCGDrawing.php');require('class/BCGpostnet.barcode.php');require_once "connect_to_mysql.php";$result = mysql_query("SELECT * FROM maillist WHERE LEFT(zip,5) = '68502'") or die(mysql_error());while ($row = mysql_fetch_array($result)) {  $zip11 = $row['postnet ip'];  include "postnet2.php";  echo '<img src="postnet.png" alt="some_text" > <br/>'; }  ?>

:This script produces the same problem except it displays the fourth item selected four times:

<?phprequire('class/BCGFont.php');require('class/BCGColor.php');require('class/BCGDrawing.php');require('class/BCGpostnet.barcode.php');require_once "connect_to_mysql.php";$result = mysql_query("SELECT * FROM maillist WHERE LEFT(zip,5) = '68502'") or die(mysql_error());$row = mysql_fetch_array($result);$zip11 = $row['postnet ip'];include "postnet2.php";echo '<img src="postnet.png" alt="some_text" > <br/>'; $row = mysql_fetch_array($result);$zip11 = $row['postnet ip'];include "postnet2.php";echo '<img src="postnet.png" alt="some_text" > <br/>'; $row = mysql_fetch_array($result);$zip11 = $row['postnet ip'];include "postnet2.php";echo '<img src="postnet.png" alt="some_text" > <br/>'; $row = mysql_fetch_array($result);$zip11 = $row['postnet ip'];include "postnet2.php";echo '<img src="postnet.png" alt="some_text" > <br/>';   ?>

Link to comment
Share on other sites

All img tags point to the same thing. The script might run and generate the image each time, and overwrite the last, but by the time the browser gets the HTML code and asks for the image it's going to be set to whatever the last one was. You need some way to tell postnet.php which record to generate the image for, or have it create unique images instead of overwriting the same one.

Link to comment
Share on other sites

Sounds like I need a new table with an id that can be used as a key, to connect with relational data, and a field for a uniquely named image files. I'd send all the images to a folder and clean it out when I'm done by unlinking selected images. It would be a little more work, but I know what the problem is thanks to you. Is this the kind of solution you had in mind?

Link to comment
Share on other sites

If the images need to be static that's probably the way to do it. If you can get by with dynamic images, then you don't need to save any image files, you can just point the img tag to the PHP script and pass it the ID to generate the image for, and have it output the image straight to the browser.

Link to comment
Share on other sites

I've got it to work with unique filenames. Isn't one of the points of this topic, that dynamic images won't work? If not, how should I approach the problem?

Link to comment
Share on other sites

Is there a reference that'll let me determine the notation I need in my situation? It looks like something I might see in a URL and I can't make the connection.

Link to comment
Share on other sites

That URL would point to a PHP script where the output is an image. The script would get the ID from $_GET, look up the record in the database, create the image and output it to the browser. If you're using a library to generate the bar code images, it might have an option to output to the browser instead of save the file to disk.

Link to comment
Share on other sites

No, it's just a way to pass information to the script. The PHP script that outputs the img tags would output a URL that looks like that, and replace each ID with the ID of the record for which to display the image. You can't have an image URL like that pass post data, only querystring data.

Link to comment
Share on other sites

That requires a column available with filename for the image that's connected to the id that's being selected, right?

Link to comment
Share on other sites

I don't know how your database is structured, but it only requires a unique ID for the record to generate the image for. If you're doing it this way you wouldn't save any images at all on disk. If you have static filenames there's no reason to use a dynamic script.

Link to comment
Share on other sites

Agreed. I currently have a table with an id and the data that's needed to produce the barcode. I select the table and send the data to a class in a while loop using a mysql_fetch_array.It has a header command that I might be able to make use of since it's how this script displays the barcode, when I'm not saving it to a file.Here's that script (see: include "postnet2.php"; in the first thread)

<?php    EDIT:  How do you position a header?require('class/BCGFont.php');require('class/BCGColor.php');require('class/BCGDrawing.php');require('class/BCGpostnet.barcode.php');$font = new BCGFont('./class/font/Arial.ttf', 18);$color_black = new BCGColor(0, 0, 0);$color_white = new BCGColor(255, 255, 255);$code = new BCGpostnet();$code->setScale(2);$code->setThickness(30);$code->setForegroundColor($color_black);$code->setBackgroundColor($color_white);$code->setFont($font);$code->parse('98000');// Drawing Part$drawing = new BCGDrawing('', $color_white);$drawing->setBarcode($code);$drawing->draw();header('Content-Type: image/png');$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);?> 

:

Link to comment
Share on other sites

How do you position something that's displayed by a header script?

header('Content-Type: image/png');

Link to comment
Share on other sites

What do you mean position it? Position the image on the page? The PHP script is only used to generate the image, it only replaces the URL of the image source. Everything else is still HTML and CSS, you're just using a different type of image. It's no different than switching from a gif to a png, as far as the web page goes. Go look at Synook's signature and right-click on one of the headlines and look at what it is, it's an image that points to a PHP script so that the filename stays the same but the image might be different (in his case, it's just an image of text).

Link to comment
Share on other sites

I went to Synook's profile. What do you mean by signature and headlines?

Link to comment
Share on other sites

I'm just trying to point out examples of dynamic images, he has dynamic images in his signature. Don't worry about that, focus on the concept of using a PHP script to generate and output an image, and telling that script which image to generate. Those are the only two pieces, you already have the script that creates images and you can use the querystring like I showed to pass it information, like which image to generate.

Link to comment
Share on other sites

Right. Also that script displays the image it creates using the header script. I'm just asking if it's possible to move the image around the page when the image's displayed using header. If so, I don't know how to do that. Perhaps that's a question for the CSS forum.

Link to comment
Share on other sites

In terms of HTML and CSS, there is exactly zero difference between an image generated by PHP and any other image. If you already know how to position a JPG on the page, you don't need to learn anything else.

Link to comment
Share on other sites

I know how to display a JPG with CSS & HTML, but I'm not clear about how to display an image created by PHP. What would the HTML/CSS/PHP even look like?

echo '<img style="float:right;" src="header('Content-Type: image/png')">';

The above script didn't work, but am I on the right track? How should the HTML/CSS be added?The header function is from this script (which does work)

<?phprequire('class/BCGFont.php');require('class/BCGColor.php');require('class/BCGDrawing.php');require('class/BCGpostnet.barcode.php');$font = new BCGFont('./class/font/Arial.ttf', 18);$color_black = new BCGColor(0, 0, 0);$color_white = new BCGColor(255, 255, 255);$code = new BCGpostnet();$code->setScale(2);$code->setThickness(30);$code->setForegroundColor($color_black);$code->setBackgroundColor($color_white);$code->setFont($font);$code->parse('98000');// Drawing Part$drawing = new BCGDrawing('', $color_white);$drawing->setBarcode($code);$drawing->draw();header('Content-Type: image/png');$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);?> 

:

Link to comment
Share on other sites

Is this more on the right track:

<?php echo 'img< src="include "postnet.php"">'; ?>

It doesn't work, but if it's on the right track, what changes does it need?

Link to comment
Share on other sites

The img tag is not formed correctly. Look at the brackets and quotes. And no, you don't use include or any other PHP code in the src attribute. You just point it at the script that generates the image. Consider the PHP script an image, and use it as the src. Look at post 6.

Link to comment
Share on other sites

I see said the blindmam:

echo '<img src="postnet.php"/>'

Thanks justsomeguy for your patience.Niche

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...