Jump to content

displaying a category name


An7hony

Recommended Posts

I am trying to display a category name related to a 'job' post.

 

The function i create the job with is shown below:

 

<?php
function create_job($params)
{
   db_connect();

	$query =  sprintf("INSERT INTO jobs set 
						jobs.agent = '%s',
		                jobs.branch = '%s',
						jobs.title = '%s',
						jobs.location = '%s',  
						jobs.salary_from = '%s',
						jobs.salary_to = '%s',
						jobs.frequency = '%s',
						jobs.jobtype = '%s',  
                            jobs.description = '%s',
					    jobs.specifications = '%s',
					    jobs.benefits = '%s',
					    jobs.email = '%s',
											  created_at = NOW() 
								", mysql_real_escape_string($params['agent']),
									 mysql_real_escape_string($params['branch']),
								   mysql_real_escape_string($params['title']),
									 mysql_real_escape_string($params['location']),
									 mysql_real_escape_string($params['salary_from']),
									 mysql_real_escape_string($params['salary_to']),
									 mysql_real_escape_string($params['frequency']),
									 mysql_real_escape_string($params['jobtype']),
									 mysql_real_escape_string($params['description']),
									 mysql_real_escape_string($params['specifications']),
									 mysql_real_escape_string($params['benefits']),
									 mysql_real_escape_string($params['email'])

								);

	$result = mysql_query($query);
	if(!$result)
	{
		 return false;
	}

	$job_id = mysql_insert_id();

	if(!empty($params['cats'])) 
	{
       foreach($params['cats'] as $cat_id) 
		 {
			 $query = sprintf("INSERT 
				                     INTO jobs2categories
				                          set
																	 category_id = '%s',
																	 job_id = '%s'",
																	 mysql_real_escape_string($cat_id),
																	 mysql_real_escape_string($job_id)
																	 );

			 $result = mysql_query($query);

			 if(!$result)
			 {
				 return false; 
			 }

		 }

   	}

	 return true;

}	?>

 

Here is the code i use to pull the job data:

 

<?php $query = "SELECT id, agent, branch, title, location, salary_from, salary_to, frequency, jobtype, description, specifications, benefits, email, created_at FROM jobs WHERE branch = '$ulevel' ORDER BY id DESC ";
$result = mysql_query($query) or die('Error : ' . mysql_error());

if(mysql_num_rows($result)==0) {
    echo('
<span><strong>No Jobs from '.$ulevel.' Listed</strong></span>
');
}

while(list($id, $agent, $branch, $title, $location, $salary_from, $salary_to, $frequency, $jobtype, $description, $specifications, $benefits, $email, $created_at) = mysql_fetch_array($result, MYSQL_NUM))
{
?>
      				<tr>
      					
                        <td class="col-first"><?php echo date('d.m.Y',strtotime($created_at));?></td>
      					<td class="col-second">### CATEGORY NAME ###</td>
      					<td class="col-third"><?php echo safe_output($title); ?></td>
                        <td class="col-fourth"><?php echo safe_output($location); ?></td>
                        <td class="col-five"><?php echo safe_output($jobtype); ?></td>
                        <td class="col-sixth"><?php echo safe_output($agent); ?></td>
                        <td class="col-seven">3</td>
      					<td class="row-nav"><a href="#" class="table-edit-link">Edit</a> <span class="hidden"> | </span> <a href="javascript:delArticle('<?php echo safe_output($id); ?>');" class="table-delete-link">Delete</a></td>
      				</tr>
				<?php
}



?>

 

How would i display the related category name in the table above?

 

Any help is appreciated

Link to comment
Share on other sites

if this is more help here is the form:

<?
<li class="even"><label class="field-title">Categories: </label> <label>
						<?  $categories = find_categories(); ?>
						<?php foreach( $categories as $category): ?>
			<input type="checkbox" name="job[cats][]" value="<?php echo $category['id']; ?>" > <?php echo $category['name']; ?> <br/>
         <?php endforeach; ?></label> 
						<span class="clearFix"> </span></li>
?>

 

and here is the find_categories(); function

<?
function find_categories()
{
   db_connect();

	$query =  "SELECT 
                  categories.name, 
								categories.id,
								COUNT(jobs2categories.job_id) as numjobs
						  FROM
							  categories
						  LEFT JOIN
							   jobs2categories ON categories.id = jobs2categories.category_id
							GROUP BY
							    categories.id
							 ORDER BY
							    categories.name ASC
								";

	$result = mysql_query($query);

	$result = db_result_to_array($result);

	return $result;

}
?>

Link to comment
Share on other sites

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.