Jump to content


Photo

php & mysql editing returned values


  • Please log in to reply
9 replies to this topic

#1 Munch

Munch
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 October 2006 - 09:59 AM

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

#2 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 06 October 2006 - 10:05 AM

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

Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 06 October 2006 - 10:07 AM

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

Jobs





IDNumberName
1111Clean Windows
2211Wash Clothes
3311Empty Bins


If so then yes, it's quite simple.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 Munch

Munch
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 October 2006 - 10:13 AM

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

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 06 October 2006 - 10:20 AM

OK, if you don't have the information in the database then you'll need something like an array with the data in.

<?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]}";
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 Munch

Munch
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 October 2006 - 10:58 AM

hi huggie,

how would i display that script in a imagecreatefrompng image please?

sorry to be a pain
chris

#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 06 October 2006 - 11:24 AM

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:

<?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
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#8 Munch

Munch
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 October 2006 - 11:27 AM

This is what i have so far...

staff.php

<?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);
	}
  }
?>

staff.inc.php

<?php

$pngimage = "staff.png";

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

?>

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

#9 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 06 October 2006 - 11:33 AM

OK, change this:

<?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);
}
?>

To this:

<?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);
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#10 Munch

Munch
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 October 2006 - 11:48 AM

thank you very much :) its working how i wanted it to now :D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users