Jump to content

Recommended Posts

Hi guys,

Im new to php and have been working on a image that returns infomation from a mysql database,
In mysql i have a list of jobs which are numbered 111, 211, 311 etc
What i want to do is be able to convert those numbers into the names of the actual job and display the name in the image.

Note: Editing the numbers in the database is not a option

Thank you for any help that you can provide.
Chris
Link to comment
https://forums.phpfreaks.com/topic/23161-php-mysql-editing-returned-values/
Share on other sites

Hi

Am i missing something?

Is your table struc something like?

jobid  |  jobname    |    data
111    |  something  |    random
211    |  blahblah      |      data
311    |  etc              |      here

then just select jobname instead of jobid?

if it isn't that is probably what you should be doing...

cheers,
tdw
I'm assuming that you have the names of the job in your database e.g.

[b]Jobs[/b]
[table]
[tr][td]ID[/td][td]Number[/td][td]Name[/td][/tr]
[tr][td]1[/td][td]111[/td][td]Clean Windows[/td][/tr]
[tr][td]2[/td][td]211[/td][td]Wash Clothes[/td][/tr]
[tr][td]3[/td][td]311[/td][td]Empty Bins[/td][/tr]
[/table]

If so then yes, it's quite simple.

Regards
Huggie
The only infomation that is in the database except personal infomation is the job numbers

In the DB..
Name| Job | Phone
john | 111 | +44
dave| 211 | +44
rich  | 311 | +44

Not in the DB
111 = Plumber
211 = Electrician
311 = Joiner

I want to be able to call the infomaton from the database by http://domain.com/job.php?name=John
then have the business card shown with name job title andphone number

I hope this clears things up :)
Chris
OK, if you don't have the information in the database then you'll need something like an array with the data in.

[code]
<?php
$profession = array(111 => "Plumber", 211 => "Electrician", 311 => "Joiner");

$sql = "SELECT job FROM tablename";
$result = mysql_query($sql);
while ($roles = $mysql_fetch_array($result, MYSQL_ASSOC)){
  extract($roles);
  echo "{$job} => {$profession[$job]}";
}
?>
[/code]

Regards
Huggie
You wouldn't... You just put this code around any image creation code and then use the variables with the image functions, something like this:

[code]<?php
$profession = array(111 => "Plumber", 211 => "Electrician", 311 => "Joiner");

$sql = "SELECT job FROM tablename";
$result = mysql_query($sql);
while ($roles = $mysql_fetch_array($result, MYSQL_ASSOC)){
  extract($roles);
  $text = $profession[$job];
  // All your image code goes here.  You should use imagettftext() to add the text to the image
  // and you'll want to provide the last argument, which is string as $text
}
?>[/code]

Regards
Huggie
This is what i have so far...

[b]staff.php[/b]
[code]

<?php

  error_reporting(0);
  include ("staff.inc.php");

  $name = $_GET['name'];
  if ($name == null || $name == '') {
    $name = "";
  }

$link = mysql_connect($MySQLSrv, $MySQLUser, $MySQLPass);
if (!$link) {
  die('Could not connect to MySQL! : ' . mysql_error());
}

$db_selected = mysql_select_db($MySQLDB, $link);
if (!$db_selected) {
  die ('Could not find the DB : ' . mysql_error());
}

if ($name != "") {
$result = mysql_query("SELECT name, job, phone FROM staff WHERE name = '{$name}'");
if (!result) {
  echo 'Could not run query: ' . mysql_error();
  exit;
}
$row = mysql_fetch_row($result);

  header("Content-type: image/png");
  $im    = imagecreatefrompng("$pngimage");
  $cBlack = imagecolorallocate($im, 0, 0, 0);

  if ($name != "") {
    imagestring($im, 5, 10, 10, 'Name: ' . $row[0], $cBlack);
    imagestring($im, 5, 10, 30, 'Job: ' . $row[1], $cBlack);
    imagestring($im, 5, 10, 50, 'Phone: ' . $row[2], $cBlack);
    imagepng($im);
    imagedestroy($im);
}
  }
?>[/code]

[b]staff.inc.php[/b]

[code]<?php

$pngimage = "staff.png";

$MySQLSrv="localhost";
$MySQLDB="test";
$MySQLUser="root";
$MySQLPass="";

?>[/code]

i was typin this out while you were posting.. ill ready ur post now :)
OK, change this:

[code]
<?php
if ($name != "") {
  imagestring($im, 5, 10, 10, 'Name: ' . $row[0], $cBlack);
  imagestring($im, 5, 10, 30, 'Job: ' . $row[1], $cBlack);
  imagestring($im, 5, 10, 50, 'Phone: ' . $row[2], $cBlack);
  imagepng($im);
  imagedestroy($im);
}
?>
[/code]

To this:

[code]
<?php
if ($name != "") {
  imagestring($im, 5, 10, 10, 'Name: ' . $row[0], $cBlack);
  $profession = array(111 => "Plumber", 211 => "Electrician", 311 => "Joiner");
  $job_id = $row[1];
  $job = $profession[$job_id];
  imagestring($im, 5, 10, 30, 'Job: ' . $job, $cBlack);
  imagestring($im, 5, 10, 50, 'Phone: ' . $row[2], $cBlack);
  imagepng($im);
  imagedestroy($im);
}
?>
[/code]

Regards
Huggie
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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